Files
swift-certificates/Sources/X509/RandomNumberGenerator+bytes.swift
David Nadoba 45ad9935c7 Adopt apple/swift-format (#121)
* Adopt `apple/swift-format`

* install swift-format

* set `UseSynthesizedInitializer` to `false` until bug is fixed https://github.com/apple/swift-format/issues/591

* fix license header

* Fix license header
2023-08-16 11:17:34 +01:00

40 lines
1.2 KiB
Swift

//===----------------------------------------------------------------------===//
//
// This source file is part of the SwiftCertificates open source project
//
// Copyright (c) 2023 Apple Inc. and the SwiftCertificates project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of SwiftCertificates project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
extension RandomNumberGenerator {
@inlinable
internal mutating func bytes(count: Int) -> ArraySlice<UInt8> {
precondition(count >= 0)
var bytes = [UInt8]()
while bytes.count < count {
bytes.appendLittleEndianBytes(self.next())
}
return bytes[..<count]
}
}
extension Array where Element == UInt8 {
@inlinable
mutating func appendLittleEndianBytes(_ number: UInt64) {
let number = number.littleEndian
for byte in 0..<(MemoryLayout<UInt64>.size) {
let shifted = number >> (byte * 8)
self.append(UInt8(truncatingIfNeeded: shifted))
}
}
}