Skip to content

Commit

Permalink
Refactored integer casting to use built-in functions
Browse files Browse the repository at this point in the history
  • Loading branch information
dimitribouniol committed May 8, 2024
1 parent ecfe814 commit df18fe6
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 36 deletions.
4 changes: 2 additions & 2 deletions Sources/WebAuthn/Ceremonies/Shared/AuthenticatorData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ extension AuthenticatorData {

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

var remainingCount = bytes.count - minAuthDataLength

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

guard idLength <= 1023
else { throw WebAuthnError.credentialIDTooLong }
Expand Down
16 changes: 16 additions & 0 deletions Sources/WebAuthn/Helpers/ByteCasting.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,19 @@ extension BidirectionalCollection where Element == UInt8 {
return result
}
}

extension FixedWidthInteger {
/// Initialize a fixed width integer from a contiguous sequence of Bytes representing a big endian type.
/// - Parameter bigEndianBytes: The Bytes to interpret as a big endian integer.
@inlinable
init(bigEndianBytes: some BidirectionalCollection<UInt8>) {
self.init(bigEndian: bigEndianBytes.casting())
}

/// Initialize a fixed width integer from a contiguous sequence of Bytes representing a little endian type.
/// - Parameter bigEndianBytes: The Bytes to interpret as a little endian integer.
@inlinable
init(littleEndianBytes: some BidirectionalCollection<UInt8>) {
self.init(littleEndian: littleEndianBytes.casting())
}
}
34 changes: 0 additions & 34 deletions Sources/WebAuthn/Helpers/Numbers+Bytes.swift

This file was deleted.

0 comments on commit df18fe6

Please sign in to comment.