Skip to content

Commit 595e317

Browse files
committed
Rename internal function Crypto.hash to sha256
1 parent 3831bfe commit 595e317

File tree

10 files changed

+31
-30
lines changed

10 files changed

+31
-30
lines changed

webauthn-server-core/src/main/java/com/yubico/webauthn/AndroidSafetynetAttestationStatementVerifier.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public boolean verifyAttestationSignature(
6868

6969
ByteArray signedData =
7070
attestationObject.getAuthenticatorData().getBytes().concat(clientDataJsonHash);
71-
ByteArray hashSignedData = Crypto.hash(signedData);
71+
ByteArray hashSignedData = Crypto.sha256(signedData);
7272
ByteArray nonceByteArray = ByteArray.fromBase64(payload.get("nonce").textValue());
7373
ExceptionUtil.assure(
7474
hashSignedData.equals(nonceByteArray),

webauthn-server-core/src/main/java/com/yubico/webauthn/Crypto.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -137,12 +137,12 @@ public static boolean verifySignature(
137137
}
138138
}
139139

140-
public static ByteArray hash(ByteArray bytes) {
140+
public static ByteArray sha256(ByteArray bytes) {
141141
//noinspection UnstableApiUsage
142142
return new ByteArray(Hashing.sha256().hashBytes(bytes.getBytes()).asBytes());
143143
}
144144

145-
public static ByteArray hash(String str) {
146-
return hash(new ByteArray(str.getBytes(StandardCharsets.UTF_8)));
145+
public static ByteArray sha256(String str) {
146+
return sha256(new ByteArray(str.getBytes(StandardCharsets.UTF_8)));
147147
}
148148
}

webauthn-server-core/src/main/java/com/yubico/webauthn/FinishAssertionSteps.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -421,15 +421,15 @@ class Step11 implements Step<Step12> {
421421
public void validate() {
422422
try {
423423
assure(
424-
Crypto.hash(rpId)
424+
Crypto.sha256(rpId)
425425
.equals(response.getResponse().getParsedAuthenticatorData().getRpIdHash()),
426426
"Wrong RP ID hash.");
427427
} catch (IllegalArgumentException e) {
428428
Optional<AppId> appid =
429429
request.getPublicKeyCredentialRequestOptions().getExtensions().getAppid();
430430
if (appid.isPresent()) {
431431
assure(
432-
Crypto.hash(appid.get().getId())
432+
Crypto.sha256(appid.get().getId())
433433
.equals(response.getResponse().getParsedAuthenticatorData().getRpIdHash()),
434434
"Wrong RP ID hash.");
435435
} else {
@@ -537,7 +537,7 @@ public Step16 nextStep() {
537537
}
538538

539539
public ByteArray clientDataJsonHash() {
540-
return Crypto.hash(response.getResponse().getClientDataJSON());
540+
return Crypto.sha256(response.getResponse().getClientDataJSON());
541541
}
542542
}
543543

webauthn-server-core/src/main/java/com/yubico/webauthn/FinishRegistrationSteps.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ public Step8 nextStep() {
259259
}
260260

261261
public ByteArray clientDataJsonHash() {
262-
return Crypto.hash(response.getResponse().getClientDataJSON());
262+
return Crypto.sha256(response.getResponse().getClientDataJSON());
263263
}
264264
}
265265

@@ -292,7 +292,7 @@ class Step9 implements Step<Step10> {
292292
@Override
293293
public void validate() {
294294
assure(
295-
Crypto.hash(rpId)
295+
Crypto.sha256(rpId)
296296
.equals(response.getResponse().getAttestation().getAuthenticatorData().getRpIdHash()),
297297
"Wrong RP ID hash.");
298298
}

webauthn-server-core/src/test/scala/com/yubico/webauthn/PackedAttestationStatementVerifierSpec.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ class PackedAttestationStatementVerifierSpec
8282

8383
val result = verifier.verifyAttestationSignature(
8484
credential.getResponse.getAttestation,
85-
Crypto.hash(credential.getResponse.getClientDataJSON),
85+
Crypto.sha256(credential.getResponse.getClientDataJSON),
8686
)
8787

8888
key.getAlgorithm should be("EC")
@@ -100,7 +100,7 @@ class PackedAttestationStatementVerifierSpec
100100

101101
val result = verifier.verifyAttestationSignature(
102102
credential.getResponse.getAttestation,
103-
Crypto.hash(credential.getResponse.getClientDataJSON),
103+
Crypto.sha256(credential.getResponse.getClientDataJSON),
104104
)
105105

106106
key.getAlgorithm should be("RSA")

webauthn-server-core/src/test/scala/com/yubico/webauthn/RegistrationTestData.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -510,7 +510,7 @@ case class RegistrationTestData(
510510
def clientDataJsonBytes: ByteArray =
511511
new ByteArray(clientDataJson.getBytes("UTF-8"))
512512
def clientData = new CollectedClientData(clientDataJsonBytes)
513-
def clientDataJsonHash: ByteArray = Crypto.hash(clientDataJsonBytes)
513+
def clientDataJsonHash: ByteArray = Crypto.sha256(clientDataJsonBytes)
514514
def aaguid: ByteArray =
515515
new AttestationObject(
516516
attestationObject

webauthn-server-core/src/test/scala/com/yubico/webauthn/RelyingPartyAssertionSpec.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ class RelyingPartyAssertionSpec
7373

7474
private def jsonFactory: JsonNodeFactory = JsonNodeFactory.instance
7575

76-
private def sha256(bytes: ByteArray): ByteArray = Crypto.hash(bytes)
76+
private def sha256(bytes: ByteArray): ByteArray = Crypto.sha256(bytes)
7777
private def sha256(data: String): ByteArray =
7878
sha256(new ByteArray(data.getBytes(Charset.forName("UTF-8"))))
7979

@@ -1419,7 +1419,7 @@ class RelyingPartyAssertionSpec
14191419

14201420
it("A test case with a different signed RP ID hash fails.") {
14211421
val rpId = "ARGHABLARGHLER"
1422-
val rpIdHash: ByteArray = Crypto.hash(rpId)
1422+
val rpIdHash: ByteArray = Crypto.sha256(rpId)
14231423
val steps = finishAssertion(
14241424
authenticatorData = new ByteArray(
14251425
(rpIdHash.getBytes.toVector ++ Defaults.authenticatorData.getBytes.toVector
@@ -1499,7 +1499,7 @@ class RelyingPartyAssertionSpec
14991499
)
15001500
val signature = TestAuthenticator.makeAssertionSignature(
15011501
authenticatorData,
1502-
Crypto.hash(Defaults.clientDataJsonBytes),
1502+
Crypto.sha256(Defaults.clientDataJsonBytes),
15031503
Defaults.credentialKey.getPrivate,
15041504
)
15051505

webauthn-server-core/src/test/scala/com/yubico/webauthn/RelyingPartyRegistrationSpec.scala

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ class RelyingPartyRegistrationSpec
8686
private def toJson(obj: Map[String, String]): JsonNode =
8787
toJsonObject(obj.view.mapValues(jsonFactory.textNode).toMap)
8888

89-
private def sha256(bytes: ByteArray): ByteArray = Crypto.hash(bytes)
89+
private def sha256(bytes: ByteArray): ByteArray = Crypto.sha256(bytes)
9090

9191
def flipByte(index: Int, bytes: ByteArray): ByteArray =
9292
editByte(bytes, index, b => (0xff ^ b).toByte)
@@ -1142,7 +1142,7 @@ class RelyingPartyRegistrationSpec
11421142
RegistrationTestData.FidoU2f.BasicAttestation
11431143
)
11441144
val step: FinishRegistrationSteps#Step14 = new steps.Step14(
1145-
Crypto.hash(
1145+
Crypto.sha256(
11461146
new ByteArray(
11471147
testData.clientDataJsonBytes.getBytes.updated(
11481148
20,
@@ -1169,7 +1169,7 @@ class RelyingPartyRegistrationSpec
11691169
credentialId = Some(new ByteArray(Array.fill(16)(0))),
11701170
)
11711171
val step: FinishRegistrationSteps#Step14 = new steps.Step14(
1172-
Crypto.hash(testData.clientDataJsonBytes),
1172+
Crypto.sha256(testData.clientDataJsonBytes),
11731173
new AttestationObject(testData.attestationObject),
11741174
Some(new FidoU2fAttestationStatementVerifier).asJava,
11751175
Nil.asJava,
@@ -1218,7 +1218,7 @@ class RelyingPartyRegistrationSpec
12181218
credentialId = Some(new ByteArray(Array.fill(16)(0))),
12191219
)
12201220
val step: FinishRegistrationSteps#Step14 = new steps.Step14(
1221-
Crypto.hash(testData.clientDataJsonBytes),
1221+
Crypto.sha256(testData.clientDataJsonBytes),
12221222
new AttestationObject(testData.attestationObject),
12231223
Some(new FidoU2fAttestationStatementVerifier).asJava,
12241224
Nil.asJava,
@@ -1268,7 +1268,7 @@ class RelyingPartyRegistrationSpec
12681268
new FidoU2fAttestationStatementVerifier()
12691269
.verifyAttestationSignature(
12701270
credential.getResponse.getAttestation,
1271-
Crypto.hash(credential.getResponse.getClientDataJSON),
1271+
Crypto.sha256(credential.getResponse.getClientDataJSON),
12721272
)
12731273
}
12741274

@@ -1320,7 +1320,7 @@ class RelyingPartyRegistrationSpec
13201320
new FidoU2fAttestationStatementVerifier()
13211321
.verifyAttestationSignature(
13221322
credential.getResponse.getAttestation,
1323-
Crypto.hash(credential.getResponse.getClientDataJSON),
1323+
Crypto.sha256(credential.getResponse.getClientDataJSON),
13241324
)
13251325
}
13261326

@@ -1372,7 +1372,7 @@ class RelyingPartyRegistrationSpec
13721372

13731373
val steps = finishRegistration(testData = testData)
13741374
val step: FinishRegistrationSteps#Step14 = new steps.Step14(
1375-
Crypto.hash(testData.clientDataJsonBytes),
1375+
Crypto.sha256(testData.clientDataJsonBytes),
13761376
new AttestationObject(testData.attestationObject),
13771377
Some(new NoneAttestationStatementVerifier).asJava,
13781378
Nil.asJava,

webauthn-server-core/src/test/scala/com/yubico/webauthn/TestAuthenticator.scala

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -579,7 +579,7 @@ object TestAuthenticator {
579579
.signature(
580580
makeAssertionSignature(
581581
authDataBytes,
582-
Crypto.hash(clientDataJsonBytes),
582+
Crypto.sha256(clientDataJsonBytes),
583583
credentialKey.getPrivate,
584584
alg,
585585
)
@@ -611,7 +611,7 @@ object TestAuthenticator {
611611
new ByteArray(
612612
(Vector[Byte](0)
613613
++ rpIdHash.getBytes
614-
++ Crypto.hash(clientDataJson).getBytes
614+
++ Crypto.sha256(clientDataJson).getBytes
615615
++ credentialId.getBytes
616616
++ credentialPublicKeyRawBytes.getBytes).toArray
617617
)
@@ -655,7 +655,7 @@ object TestAuthenticator {
655655
signer: AttestationSigner,
656656
): JsonNode = {
657657
val signedData = new ByteArray(
658-
authDataBytes.getBytes ++ Crypto.hash(clientDataJson).getBytes
658+
authDataBytes.getBytes ++ Crypto.sha256(clientDataJson).getBytes
659659
)
660660
val signature = signer match {
661661
case SelfAttestation(keypair, alg) =>
@@ -693,7 +693,8 @@ object TestAuthenticator {
693693
cert: AttestationCert,
694694
ctsProfileMatch: Boolean = true,
695695
): JsonNode = {
696-
val nonce = Crypto.hash(authDataBytes concat Crypto.hash(clientDataJson))
696+
val nonce =
697+
Crypto.sha256(authDataBytes concat Crypto.sha256(clientDataJson))
697698

698699
val f = JsonNodeFactory.instance
699700

@@ -722,11 +723,11 @@ object TestAuthenticator {
722723
"nonce" -> f.textNode(nonce.getBase64),
723724
"timestampMs" -> f.numberNode(Instant.now().toEpochMilli),
724725
"apkPackageName" -> f.textNode("com.yubico.webauthn.test"),
725-
"apkDigestSha256" -> f.textNode(Crypto.hash("foo").getBase64),
726+
"apkDigestSha256" -> f.textNode(Crypto.sha256("foo").getBase64),
726727
"ctsProfileMatch" -> f.booleanNode(ctsProfileMatch),
727728
"aplCertificateDigestSha256" -> f
728729
.arrayNode()
729-
.add(f.textNode(Crypto.hash("foo").getBase64)),
730+
.add(f.textNode(Crypto.sha256("foo").getBase64)),
730731
"basicIntegrity" -> f.booleanNode(true),
731732
).asJava
732733
)

webauthn-server-demo/src/main/java/com/yubico/webauthn/U2fVerifier.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,9 @@ public class U2fVerifier {
4444
public static boolean verify(
4545
AppId appId, RegistrationRequest request, U2fRegistrationResponse response)
4646
throws CertificateException, IOException, Base64UrlException {
47-
final ByteArray appIdHash = Crypto.hash(appId.getId());
47+
final ByteArray appIdHash = Crypto.sha256(appId.getId());
4848
final ByteArray clientDataHash =
49-
Crypto.hash(response.getCredential().getU2fResponse().getClientDataJSON());
49+
Crypto.sha256(response.getCredential().getU2fResponse().getClientDataJSON());
5050

5151
final JsonNode clientData =
5252
JacksonCodecs.json()

0 commit comments

Comments
 (0)