Skip to content

Commit 6da5de2

Browse files
Refactored integer casting to use built-in functions
1 parent 624f866 commit 6da5de2

File tree

3 files changed

+18
-36
lines changed

3 files changed

+18
-36
lines changed

Sources/WebAuthn/Ceremonies/Shared/AuthenticatorData.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ extension AuthenticatorData {
3636

3737
let relyingPartyIDHash = Array(bytes[..<32])
3838
let flags = AuthenticatorFlags(bytes[32])
39-
let counter: UInt32 = Data(bytes[33..<37]).toInteger(endian: .big)
39+
let counter = UInt32(bigEndianBytes: bytes[33..<37])
4040

4141
var remainingCount = bytes.count - minAuthDataLength
4242

@@ -90,7 +90,7 @@ extension AuthenticatorData {
9090
/// **credentialIdLength** (2): Byte length L of credentialId, 16-bit unsigned big-endian integer. Value MUST be ≤ 1023.
9191
let idLengthBytes = data[53..<55] // Length is 2 bytes
9292
let idLengthData = Data(idLengthBytes)
93-
let idLength: UInt16 = idLengthData.toInteger(endian: .big)
93+
let idLength = UInt16(bigEndianBytes: idLengthData)
9494

9595
guard idLength <= 1023
9696
else { throw WebAuthnError.credentialIDTooLong }

Sources/WebAuthn/Helpers/ByteCasting.swift

+16
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,19 @@ extension BidirectionalCollection where Element == UInt8 {
3131
return result
3232
}
3333
}
34+
35+
extension FixedWidthInteger {
36+
/// Initialize a fixed width integer from a contiguous sequence of Bytes representing a big endian type.
37+
/// - Parameter bigEndianBytes: The Bytes to interpret as a big endian integer.
38+
@inlinable
39+
init(bigEndianBytes: some BidirectionalCollection<UInt8>) {
40+
self.init(bigEndian: bigEndianBytes.casting())
41+
}
42+
43+
/// Initialize a fixed width integer from a contiguous sequence of Bytes representing a little endian type.
44+
/// - Parameter bigEndianBytes: The Bytes to interpret as a little endian integer.
45+
@inlinable
46+
init(littleEndianBytes: some BidirectionalCollection<UInt8>) {
47+
self.init(littleEndian: littleEndianBytes.casting())
48+
}
49+
}

Sources/WebAuthn/Helpers/Numbers+Bytes.swift

-34
This file was deleted.

0 commit comments

Comments
 (0)