com.auth0.jwt.algorithms.Algorithm.ECDSA256()方法的使用及代码示例

x33g5p2x  于2022-01-16 转载在 其他  
字(10.8k)|赞(0)|评价(0)|浏览(306)

本文整理了Java中com.auth0.jwt.algorithms.Algorithm.ECDSA256()方法的一些代码示例,展示了Algorithm.ECDSA256()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Algorithm.ECDSA256()方法的具体详情如下:
包路径:com.auth0.jwt.algorithms.Algorithm
类名称:Algorithm
方法名:ECDSA256

Algorithm.ECDSA256介绍

[英]Creates a new Algorithm instance using SHA256withECDSA. Tokens specify this as "ES256".
[中]使用SHA256withECDSA创建新的算法实例。令牌将其指定为“ES256”。

代码示例

代码示例来源:origin: auth0/java-jwt

/**
 * Creates a new Algorithm instance using SHA256withECDSA. Tokens specify this as "ES256".
 *
 * @param publicKey  the key to use in the verify instance.
 * @param privateKey the key to use in the signing instance.
 * @return a valid ECDSA256 Algorithm.
 * @throws IllegalArgumentException if the provided Key is null.
 */
public static Algorithm ECDSA256(ECPublicKey publicKey, ECPrivateKey privateKey) throws IllegalArgumentException {
  return ECDSA256(ECDSAAlgorithm.providerForKeys(publicKey, privateKey));
}

代码示例来源:origin: auth0/java-jwt

@Test
public void shouldPassECDSA256VerificationWithProvidedPublicKey() throws Exception {
  ECDSAKeyProvider provider = mock(ECDSAKeyProvider.class);
  PublicKey publicKey = readPublicKeyFromFile(PUBLIC_KEY_FILE_256, "EC");
  when(provider.getPublicKeyById("my-key-id")).thenReturn((ECPublicKey) publicKey);
  String jwt = "eyJhbGciOiJFUzI1NiIsImtpZCI6Im15LWtleS1pZCJ9.eyJpc3MiOiJhdXRoMCJ9.D_oU4CB0ZEsxHOjcWnmS3ZJvlTzm6WcGFx-HASxnvcB2Xu2WjI-axqXH9xKq45aPBDs330JpRhJmqBSc2K8MXQ";
  Algorithm algorithm = Algorithm.ECDSA256(provider);
  algorithm.verify(JWT.decode(jwt));
}

代码示例来源:origin: auth0/java-jwt

@Test
public void shouldCreateECDSA256AlgorithmWithPrivateKey() throws Exception {
  ECKey key = mock(ECKey.class, withSettings().extraInterfaces(ECPrivateKey.class));
  Algorithm algorithm = Algorithm.ECDSA256(key);
  assertThat(algorithm, is(notNullValue()));
  assertThat(algorithm, is(instanceOf(ECDSAAlgorithm.class)));
  assertThat(algorithm.getDescription(), is("SHA256withECDSA"));
  assertThat(algorithm.getName(), is("ES256"));
}

代码示例来源:origin: auth0/java-jwt

@Test
public void shouldThrowECDSA256InstanceWithNullKeys() throws Exception {
  exception.expect(IllegalArgumentException.class);
  exception.expectMessage("Both provided Keys cannot be null.");
  Algorithm.ECDSA256(null, null);
}

代码示例来源:origin: auth0/java-jwt

@Test
public void shouldDoECDSA256Signing() throws Exception {
  Algorithm algorithmSign = Algorithm.ECDSA256((ECKey) readPrivateKeyFromFile(PRIVATE_KEY_FILE_256, "EC"));
  Algorithm algorithmVerify = Algorithm.ECDSA256((ECKey) readPublicKeyFromFile(PUBLIC_KEY_FILE_256, "EC"));
  String jwt = asJWT(algorithmSign, ES256Header, auth0IssPayload);
  assertSignaturePresent(jwt);
  algorithmVerify.verify(JWT.decode(jwt));
}

代码示例来源:origin: auth0/java-jwt

@Test
public void shouldDoECDSA256Signing() throws Exception {
  Algorithm algorithm = Algorithm.ECDSA256((ECKey) readPrivateKeyFromFile(PRIVATE_KEY_FILE_256, "EC"));
  Algorithm algorithmVerify = Algorithm.ECDSA256((ECKey) readPublicKeyFromFile(PUBLIC_KEY_FILE_256, "EC"));
  String jwt = asJWT(algorithm, ES256Header, auth0IssPayload);
  assertSignaturePresent(jwt);
  algorithmVerify.verify(JWT.decode(jwt));
}

代码示例来源:origin: auth0/java-jwt

@Test
public void shouldFailOnECDSA256SigningWhenProvidedPrivateKeyIsNull() throws Exception {
  exception.expect(SignatureGenerationException.class);
  exception.expectMessage("The Token's Signature couldn't be generated when signing using the Algorithm: SHA256withECDSA");
  exception.expectCause(isA(IllegalStateException.class));
  exception.expectCause(hasMessage(is("The given Private Key is null.")));
  ECDSAKeyProvider provider = mock(ECDSAKeyProvider.class);
  when(provider.getPrivateKey()).thenReturn(null);
  Algorithm algorithm = Algorithm.ECDSA256(provider);
  algorithm.sign(new byte[0], new byte[0]);
}

代码示例来源:origin: auth0/java-jwt

@Test
public void shouldFailECDSA256VerificationWhenProvidedPublicKeyIsNull() throws Exception {
  exception.expect(SignatureVerificationException.class);
  exception.expectMessage("The Token's Signature resulted invalid when verified using the Algorithm: SHA256withECDSA");
  exception.expectCause(isA(IllegalStateException.class));
  exception.expectCause(hasMessage(is("The given Public Key is null.")));
  ECDSAKeyProvider provider = mock(ECDSAKeyProvider.class);
  when(provider.getPublicKeyById("my-key-id")).thenReturn(null);
  String jwt = "eyJhbGciOiJFUzI1NiIsImtpZCI6Im15LWtleS1pZCJ9.eyJpc3MiOiJhdXRoMCJ9.D_oU4CB0ZEsxHOjcWnmS3ZJvlTzm6WcGFx-HASxnvcB2Xu2WjI-axqXH9xKq45aPBDs330JpRhJmqBSc2K8MXQ";
  Algorithm algorithm = Algorithm.ECDSA256(provider);
  algorithm.verify(JWT.decode(jwt));
}

代码示例来源:origin: auth0/java-jwt

@Test
public void shouldPassECDSA256VerificationWithJOSESignature() throws Exception {
  String jwt = "eyJhbGciOiJFUzI1NiJ9.eyJpc3MiOiJhdXRoMCJ9.4iVk3-Y0v4RT4_9IaQlp-8dZ_4fsTzIylgrPTDLrEvTHBTyVS3tgPbr2_IZfLETtiKRqCg0aQ5sh9eIsTTwB1g";
  ECKey key = (ECKey) readPublicKeyFromFile(PUBLIC_KEY_FILE_256, "EC");
  Algorithm algorithm = Algorithm.ECDSA256(key);
  algorithm.verify(JWT.decode(jwt));
}

代码示例来源:origin: auth0/java-jwt

@Test
public void shouldPassECDSA256VerificationWithJOSESignature() throws Exception {
  String jwt = "eyJhbGciOiJFUzI1NiJ9.eyJpc3MiOiJhdXRoMCJ9.4iVk3-Y0v4RT4_9IaQlp-8dZ_4fsTzIylgrPTDLrEvTHBTyVS3tgPbr2_IZfLETtiKRqCg0aQ5sh9eIsTTwB1g";
  ECKey key = (ECKey) readPublicKeyFromFile(PUBLIC_KEY_FILE_256, "EC");
  Algorithm algorithm = Algorithm.ECDSA256(key);
  algorithm.verify(JWT.decode(jwt));
}

代码示例来源:origin: auth0/java-jwt

@Test
public void shouldPassECDSA256VerificationWithJOSESignatureWithBothKeys() throws Exception {
  String jwt = "eyJhbGciOiJFUzI1NiJ9.eyJpc3MiOiJhdXRoMCJ9.4iVk3-Y0v4RT4_9IaQlp-8dZ_4fsTzIylgrPTDLrEvTHBTyVS3tgPbr2_IZfLETtiKRqCg0aQ5sh9eIsTTwB1g";
  Algorithm algorithm = Algorithm.ECDSA256((ECPublicKey) readPublicKeyFromFile(PUBLIC_KEY_FILE_256, "EC"), (ECPrivateKey) readPrivateKeyFromFile(PRIVATE_KEY_FILE_256, "EC"));
  algorithm.verify(JWT.decode(jwt));
}

代码示例来源:origin: auth0/java-jwt

@Test
public void shouldFailECDSA256VerificationWithInvalidPublicKey() throws Exception {
  exception.expect(SignatureVerificationException.class);
  exception.expectMessage("The Token's Signature resulted invalid when verified using the Algorithm: SHA256withECDSA");
  String jwt = "eyJhbGciOiJFUzI1NiJ9.eyJpc3MiOiJhdXRoMCJ9.W9qfN1b80B9hnMo49WL8THrOsf1vEjOhapeFemPMGySzxTcgfyudS5esgeBTO908X5SLdAr5jMwPUPBs9b6nNg";
  Algorithm algorithm = Algorithm.ECDSA256((ECKey) readPublicKeyFromFile(INVALID_PUBLIC_KEY_FILE_256, "EC"));
  algorithm.verify(JWT.decode(jwt));
}

代码示例来源:origin: auth0/java-jwt

@Test
public void shouldPassECDSA256VerificationWithJOSESignature() throws Exception {
  String token = "eyJhbGciOiJFUzI1NiJ9.eyJpc3MiOiJhdXRoMCJ9.4iVk3-Y0v4RT4_9IaQlp-8dZ_4fsTzIylgrPTDLrEvTHBTyVS3tgPbr2_IZfLETtiKRqCg0aQ5sh9eIsTTwB1g";
  ECKey key = (ECKey) readPublicKeyFromFile(PUBLIC_KEY_FILE_256, "EC");
  Algorithm algorithm = Algorithm.ECDSA256(key);
  JWTVerifier verifier = JWTVerifier.init(algorithm).withIssuer("auth0").build();
  concurrentVerify(verifier, token);
}

代码示例来源:origin: auth0/java-jwt

@Test
public void shouldSignAndVerifyWithECDSA256() throws Exception {
  ECDSAAlgorithm algorithm256 = (ECDSAAlgorithm) Algorithm.ECDSA256((ECPublicKey) readPublicKeyFromFile(PUBLIC_KEY_FILE_256, "EC"), (ECPrivateKey) readPrivateKeyFromFile(PRIVATE_KEY_FILE_256, "EC"));
  String header256 = "eyJhbGciOiJFUzI1NiJ9";
  String body = "eyJpc3MiOiJhdXRoMCJ9";
  for (int i = 0; i < 10; i++) {
    String jwt = asJWT(algorithm256, header256, body);
    algorithm256.verify(JWT.decode(jwt));
  }
}

代码示例来源:origin: auth0/java-jwt

@Test
public void shouldFailOnECDSA256SigningWhenUsingPublicKey() throws Exception {
  exception.expect(SignatureGenerationException.class);
  exception.expectMessage("The Token's Signature couldn't be generated when signing using the Algorithm: SHA256withECDSA");
  exception.expectCause(isA(IllegalStateException.class));
  exception.expectCause(hasMessage(is("The given Private Key is null.")));
  Algorithm algorithm = Algorithm.ECDSA256((ECKey) readPublicKeyFromFile(PUBLIC_KEY_FILE_256, "EC"));
  algorithm.sign(new byte[0], new byte[0]);
}

代码示例来源:origin: auth0/java-jwt

@Test
public void shouldThrowOnDERSignatureConversionIfRNumberDoesNotHaveExpectedLength() throws Exception {
  ECDSAAlgorithm algorithm256 = (ECDSAAlgorithm) Algorithm.ECDSA256((ECPublicKey) readPublicKeyFromFile(PUBLIC_KEY_FILE_256, "EC"), (ECPrivateKey) readPrivateKeyFromFile(PRIVATE_KEY_FILE_256, "EC"));
  byte[] derSignature = createDERSignature(32, false, false);
  derSignature[3] = (byte) 34;
  exception.expect(SignatureException.class);
  exception.expectMessage("Invalid DER signature format.");
  algorithm256.DERToJOSE(derSignature);
}

代码示例来源:origin: auth0/java-jwt

@Test
public void shouldThrowOnDERSignatureConversionIfSNumberDoesNotHaveExpectedLength() throws Exception {
  ECDSAAlgorithm algorithm256 = (ECDSAAlgorithm) Algorithm.ECDSA256((ECPublicKey) readPublicKeyFromFile(PUBLIC_KEY_FILE_256, "EC"), (ECPrivateKey) readPrivateKeyFromFile(PRIVATE_KEY_FILE_256, "EC"));
  byte[] derSignature = createDERSignature(32, false, false);
  derSignature[4 + 32 + 1] = (byte) 34;
  exception.expect(SignatureException.class);
  exception.expectMessage("Invalid DER signature format.");
  algorithm256.DERToJOSE(derSignature);
}

代码示例来源:origin: auth0/java-jwt

@Test
public void shouldDoECDSA256SigningWithBothKeys() throws Exception {
  Algorithm algorithm = Algorithm.ECDSA256((ECPublicKey) readPublicKeyFromFile(PUBLIC_KEY_FILE_256, "EC"), (ECPrivateKey) readPrivateKeyFromFile(PRIVATE_KEY_FILE_256, "EC"));
  String jwt = asJWT(algorithm, ES256Header, auth0IssPayload);
  assertSignaturePresent(jwt);
  algorithm.verify(JWT.decode(jwt));
}

代码示例来源:origin: auth0/java-jwt

@Test
public void shouldThrowOnDERSignatureConversionIfRNumberDoesNotHaveExpectedLength() throws Exception {
  ECDSAAlgorithm algorithm256 = (ECDSAAlgorithm) Algorithm.ECDSA256((ECPublicKey) readPublicKeyFromFile(PUBLIC_KEY_FILE_256, "EC"), (ECPrivateKey) readPrivateKeyFromFile(PRIVATE_KEY_FILE_256, "EC"));
  byte[] derSignature = createDERSignature(32, false, false);
  derSignature[3] = (byte) 34;
  exception.expect(SignatureException.class);
  exception.expectMessage("Invalid DER signature format.");
  algorithm256.DERToJOSE(derSignature);
}

代码示例来源:origin: auth0/java-jwt

@Test
public void shouldThrowOnECDSA256VerificationWithDERSignatureWithBothKeys() throws Exception {
  exception.expect(SignatureVerificationException.class);
  exception.expectMessage("The Token's Signature resulted invalid when verified using the Algorithm: SHA256withECDSA");
  exception.expectCause(isA(SignatureException.class));
  exception.expectCause(hasMessage(is("Invalid JOSE signature format.")));
  String jwt = "eyJhbGciOiJFUzI1NiJ9.eyJpc3MiOiJhdXRoMCJ9.MEYCIQDiJWTf5jS/hFPj/0hpCWn7x1n/h+xPMjKWCs9MMusS9AIhAMcFPJVLe2A9uvb8hl8sRO2IpGoKDRpDmyH14ixNPAHW";
  Algorithm algorithm = Algorithm.ECDSA256((ECPublicKey) readPublicKeyFromFile(PUBLIC_KEY_FILE_256, "EC"), (ECPrivateKey) readPrivateKeyFromFile(PRIVATE_KEY_FILE_256, "EC"));
  algorithm.verify(JWT.decode(jwt));
}

相关文章