All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.nimbusds.jose.jwk.ThumbprintUtils Maven / Gradle / Ivy

Go to download

Java library for Javascript Object Signing and Encryption (JOSE) and JSON Web Tokens (JWT)

There is a newer version: 9.48
Show newest version
package com.nimbusds.jose.jwk;


import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.LinkedHashMap;

import com.nimbusds.jose.JOSEException;
import com.nimbusds.jose.util.Base64URL;
import com.nimbusds.jose.util.StandardCharset;
import net.minidev.json.JSONObject;


/**
 * Thumbprint utilities.
 *
 * 

See RFC 7638. * * @author Vladimir Dzhuvinov * @version 2016-07-26 */ public final class ThumbprintUtils { /** * Computes the SHA-256 thumbprint for the specified JWK. * * @param jwk The JWK. Must not be {@code null}. * * @return The JWK thumbprint. * * @throws JOSEException If the SHA-256 hash algorithm is not * supported. */ public static Base64URL compute(final JWK jwk) throws JOSEException { return compute("SHA-256", jwk); } /** * Computes the thumbprint for the specified JWK. * * @param hashAlg The hash algorithm. Must not be {@code null}. * @param jwk The JWK. Must not be {@code null}. * * @return The JWK thumbprint. * * @throws JOSEException If the hash algorithm is not supported. */ public static Base64URL compute(final String hashAlg, final JWK jwk) throws JOSEException { final LinkedHashMap orderedParams = jwk.getRequiredParams(); return compute(hashAlg, orderedParams); } /** * Computes the thumbprint for the specified required JWK parameters. * * @param hashAlg The hash algorithm. Must not be {@code null}. * @param params The required JWK parameters, alphanumerically sorted * by parameter name and ready for JSON object * serialisation. Must not be {@code null}. * * @return The JWK thumbprint. * * @throws JOSEException If the hash algorithm is not supported. */ public static Base64URL compute(final String hashAlg, final LinkedHashMap params) throws JOSEException { final String json = JSONObject.toJSONString(params); final MessageDigest md; try { md = MessageDigest.getInstance(hashAlg); } catch (NoSuchAlgorithmException e) { throw new JOSEException("Couldn't compute JWK thumbprint: Unsupported hash algorithm: " + e.getMessage(), e); } md.update(json.getBytes(StandardCharset.UTF_8)); return Base64URL.encode(md.digest()); } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy