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

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

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

Algorithm.sign介绍

[英]Sign the given content using this Algorithm instance.
[中]使用此算法实例对给定内容进行签名。

代码示例

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

private String sign() throws SignatureGenerationException {
    String header = Base64.encodeBase64URLSafeString(headerJson.getBytes(StandardCharsets.UTF_8));
    String payload = Base64.encodeBase64URLSafeString(payloadJson.getBytes(StandardCharsets.UTF_8));

    byte[] signatureBytes = algorithm.sign(header.getBytes(StandardCharsets.UTF_8), payload.getBytes(StandardCharsets.UTF_8));
    String signature = Base64.encodeBase64URLSafeString((signatureBytes));

    return String.format("%s.%s.%s", header, payload, signature);
  }
}

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

public static String asJWT(Algorithm algorithm, String header, String payload) {
  byte[] signatureBytes = algorithm.sign(header.getBytes(StandardCharsets.UTF_8), payload.getBytes(StandardCharsets.UTF_8));
  String jwtSignature = Base64.encodeBase64URLSafeString(signatureBytes);
  return String.format("%s.%s.%s", header, payload, jwtSignature);
}

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

@Test
public void shouldBeEqualSignatureMethodResults() throws Exception {
  Algorithm algorithm = Algorithm.HMAC256("secret");
  byte[] header = new byte[]{0x00, 0x01, 0x02};
  byte[] payload = new byte[]{0x04, 0x05, 0x06};
  ByteArrayOutputStream bout = new ByteArrayOutputStream();
  bout.write(header);
  bout.write('.');
  bout.write(payload);
  assertThat(algorithm.sign(bout.toByteArray()), is(algorithm.sign(header, payload)));
}

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

@Test
public void shouldThrowOnSignWhenSignatureAlgorithmDoesNotExists() throws Exception {
  exception.expect(SignatureGenerationException.class);
  exception.expectMessage("The Token's Signature couldn't be generated when signing using the Algorithm: some-algorithm");
  exception.expectCause(isA(NoSuchAlgorithmException.class));
  CryptoHelper crypto = mock(CryptoHelper.class);
  when(crypto.createSignatureFor(anyString(), any(byte[].class), any(byte[].class), any(byte[].class)))
      .thenThrow(NoSuchAlgorithmException.class);
  Algorithm algorithm = new HMACAlgorithm(crypto, "some-alg", "some-algorithm", "secret".getBytes(StandardCharsets.UTF_8));
  algorithm.sign(new byte[0], new byte[0]);
}

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

@Test
public void shouldThrowOnSignWhenTheSecretIsInvalid() throws Exception {
  exception.expect(SignatureGenerationException.class);
  exception.expectMessage("The Token's Signature couldn't be generated when signing using the Algorithm: some-algorithm");
  exception.expectCause(isA(InvalidKeyException.class));
  CryptoHelper crypto = mock(CryptoHelper.class);
  when(crypto.createSignatureFor(anyString(), any(byte[].class), any(byte[].class), any(byte[].class)))
      .thenThrow(InvalidKeyException.class);
  Algorithm algorithm = new HMACAlgorithm(crypto, "some-alg", "some-algorithm", "secret".getBytes(StandardCharsets.UTF_8));
  algorithm.sign(new byte[0], new byte[0]);
}

代码示例来源: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"));
  byte[] signatureBytes = algorithm.sign(ES256HeaderBytes, auth0IssPayloadBytes);
  String jwtSignature = Base64.encodeBase64URLSafeString(signatureBytes);
  String jwt = String.format("%s.%s.%s", ES256Header, auth0IssPayload, jwtSignature);
  assertSignaturePresent(jwt);
  algorithm.verify(JWT.decode(jwt));
}

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

@Test
public void shouldFailOnECDSA384SigningWhenProvidedPrivateKeyIsNull() throws Exception {
  exception.expect(SignatureGenerationException.class);
  exception.expectMessage("The Token's Signature couldn't be generated when signing using the Algorithm: SHA384withECDSA");
  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.ECDSA384(provider);
  algorithm.sign(new byte[0], new byte[0]);
}

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

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

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

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

代码示例来源: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 shouldFailOnECDSA512SigningWhenProvidedPrivateKeyIsNull() throws Exception {
  exception.expect(SignatureGenerationException.class);
  exception.expectMessage("The Token's Signature couldn't be generated when signing using the Algorithm: SHA512withECDSA");
  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.ECDSA512(provider);
  algorithm.sign(new byte[0], new byte[0]);
}

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

@Test
public void shouldFailOnECDSA512SigningWhenProvidedPrivateKeyIsNull() throws Exception {
  exception.expect(SignatureGenerationException.class);
  exception.expectMessage("The Token's Signature couldn't be generated when signing using the Algorithm: SHA512withECDSA");
  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.ECDSA512(provider);
  algorithm.sign(new byte[0], new byte[0]);
}

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

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

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

@Test
public void shouldFailOnECDSA384SigningWhenProvidedPrivateKeyIsNull() throws Exception {
  exception.expect(SignatureGenerationException.class);
  exception.expectMessage("The Token's Signature couldn't be generated when signing using the Algorithm: SHA384withECDSA");
  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.ECDSA384(provider);
  algorithm.sign(new byte[0], new byte[0]);
}

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

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

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

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

代码示例来源: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 shouldFailOnECDSA512SigningWhenUsingPublicKey() throws Exception {
  exception.expect(SignatureGenerationException.class);
  exception.expectMessage("The Token's Signature couldn't be generated when signing using the Algorithm: SHA512withECDSA");
  exception.expectCause(isA(IllegalStateException.class));
  exception.expectCause(hasMessage(is("The given Private Key is null.")));
  Algorithm algorithm = Algorithm.ECDSA512((ECKey) readPublicKeyFromFile(PUBLIC_KEY_FILE_512, "EC"));
  algorithm.sign(new byte[0], new byte[0]);
}

代码示例来源: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 shouldFailOnECDSA512SigningWhenUsingPublicKey() throws Exception {
  exception.expect(SignatureGenerationException.class);
  exception.expectMessage("The Token's Signature couldn't be generated when signing using the Algorithm: SHA512withECDSA");
  exception.expectCause(isA(IllegalStateException.class));
  exception.expectCause(hasMessage(is("The given Private Key is null.")));
  Algorithm algorithm = Algorithm.ECDSA512((ECKey) readPublicKeyFromFile(PUBLIC_KEY_FILE_512, "EC"));
  algorithm.sign(new byte[0], new byte[0]);
}

相关文章