Java Code Examples for io.jsonwebtoken.SignatureAlgorithm#HS512

The following examples show how to use io.jsonwebtoken.SignatureAlgorithm#HS512 . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JwtUtil.java    From xmanager with Apache License 2.0 6 votes vote down vote up
/**
 * 创建jwt
 * @param id
 * @param subject
 * @param ttlMillis
 * @return
 * @throws Exception
 */
public String createJWT(String id, String subject, long ttlMillis) throws Exception {

    SignatureAlgorithm signatureAlgorithm = SignatureAlgorithm.HS512;
    long nowMillis = System.currentTimeMillis();
    Date now = new Date(nowMillis);
    SecretKey key = generalKey();
    JwtBuilder builder = Jwts.builder()
            .setId(id)
            .setIssuedAt(now)
            .setSubject(subject)
            .signWith(signatureAlgorithm, key);
    if (ttlMillis >= 0) {
        long expMillis = nowMillis + ttlMillis;
        Date exp = new Date(expMillis);
        builder.setExpiration(exp);
    }
    return builder.compact();
}
 
Example 2
Source File: ConfigJwkResolver.java    From juiser with Apache License 2.0 5 votes vote down vote up
static SignatureAlgorithm getAlgorithm(byte[] hmacSigningKeyBytes) {
    Assert.isTrue(hmacSigningKeyBytes != null && hmacSigningKeyBytes.length > 0,
        "hmacSigningBytes cannot be null or empty.");
    if (hmacSigningKeyBytes.length >= 64) {
        return SignatureAlgorithm.HS512;
    } else if (hmacSigningKeyBytes.length >= 48) {
        return SignatureAlgorithm.HS384;
    } else { //<= 32
        return SignatureAlgorithm.HS256;
    }
}
 
Example 3
Source File: JsonWebTokenUtility.java    From trivia-microservices with MIT License 5 votes vote down vote up
public JsonWebTokenUtility() {

		// THIS IS NOT A SECURE PRACTICE!
		// For simplicity, we are storing a static key here.
		// Ideally, in a microservices environment, this key would kept on a
		// config server.
		signatureAlgorithm = SignatureAlgorithm.HS512;
		String encodedKey = "L7A/6zARSkK1j7Vd5SDD9pSSqZlqF7mAhiOgRbgv9Smce6tf4cJnvKOjtKPxNNnWQj+2lQEScm3XIUjhW+YVZg==";
		secretKey = deserializeKey(encodedKey);

		// secretKey = MacProvider.generateKey(signatureAlgorithm);
	}
 
Example 4
Source File: JWTUtils.java    From datacollector with Apache License 2.0 5 votes vote down vote up
public static SignatureAlgorithm getSignatureAlgorithm(SigningAlgorithms alg) {
    switch (alg) {
      case HS256:
        return SignatureAlgorithm.HS256;
      case HS384:
        return SignatureAlgorithm.HS384;
      case HS512:
        return SignatureAlgorithm.HS512;
      case RS256:
        return SignatureAlgorithm.RS256;
      case RS384:
        return SignatureAlgorithm.RS384;
      case RS512: // NOSONAR - asking to reduce line count
        return SignatureAlgorithm.RS512;
        // The following are not JDK standard and are difficult to test, so ignoring for now.
//      case PS256:
//        return SignatureAlgorithm.PS256; //NOSONAR
//      case PS384:
//        return SignatureAlgorithm.PS384; //NOSONAR
//      case PS512:
//        return SignatureAlgorithm.PS512; //NOSONAR
//      case ES256:
//        return SignatureAlgorithm.ES256; //NOSONAR
//      case ES384:
//        return SignatureAlgorithm.ES384; //NOSONAR
//      case ES512:
//        return SignatureAlgorithm.ES512; //NOSONAR
      case NONE:
        return SignatureAlgorithm.NONE;
      default:
        throw new IllegalStateException("Unknown Signing Algorithm: " + alg.getLabel());
    }

  }
 
Example 5
Source File: JsonWebTokenConfig.java    From jobson with Apache License 2.0 4 votes vote down vote up
public SignatureAlgorithm getSignatureAlgorithm() {
    return SignatureAlgorithm.HS512;
}
 
Example 6
Source File: JsonWebTokenAuthenticatorTest.java    From jobson with Apache License 2.0 4 votes vote down vote up
private static SignatureAlgorithm getValidSignatureAlgorithm() {
    return SignatureAlgorithm.HS512;
}
 
Example 7
Source File: JsonWebTokenConfigTest.java    From jobson with Apache License 2.0 4 votes vote down vote up
private String createJWT(String secretKey, String username) {
    final byte[] decodedSecretKey = Base64.getDecoder().decode(secretKey);
    final Key secretKeyKey = new SecretKeySpec(decodedSecretKey, 0, decodedSecretKey.length, "HS512");
    final SignatureAlgorithm alg = SignatureAlgorithm.HS512;
    return Jwts.builder().setSubject(username).signWith(alg, secretKeyKey).compact();
}