Skip to content

Commit a948166

Browse files
iamcarbonabergs
authored andcommitted
Eliminate two hash allocations
1 parent 28347e3 commit a948166

File tree

2 files changed

+6
-4
lines changed

2 files changed

+6
-4
lines changed

Src/Fido2/AttestationFormat/AndroidSafetyNet.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -152,10 +152,11 @@ public override (AttestationType, X509Certificate2[]) Verify()
152152
{
153153
throw new Fido2VerificationException("Nonce value not base64string in SafetyNet attestation", ex);
154154
}
155-
156-
var dataHash = SHA256.HashData(Data);
157155

158-
if (!dataHash.AsSpan().SequenceEqual(nonceHash))
156+
Span<byte> dataHash = stackalloc byte[32];
157+
SHA256.HashData(Data, dataHash);
158+
159+
if (!dataHash.SequenceEqual(nonceHash))
159160
{
160161
throw new Fido2VerificationException($"SafetyNet response nonce / hash value mismatch, nonce {Convert.ToHexString(nonceHash)}, hash {Convert.ToHexString(dataHash)}");
161162
}

Src/Fido2/AttestationFormat/Apple.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,8 @@ X5c.Values is null || X5c.Values.Count is 0 ||
6363
var nonceToHash = Data;
6464

6565
// 4. Perform SHA-256 hash of nonceToHash to produce nonce.
66-
byte[] nonce = SHA256.HashData(nonceToHash);
66+
Span<byte> nonce = stackalloc byte[32];
67+
SHA256.HashData(nonceToHash, nonce);
6768

6869
// 5. Verify nonce matches the value of the extension with OID ( 1.2.840.113635.100.8.2 ) in credCert.
6970
var appleExtensionBytes = GetAppleAttestationExtensionValue(credCert.Extensions);

0 commit comments

Comments
 (0)