List of usage examples for org.apache.commons.codec.binary Base64 encodeBase64URLSafe
public static byte[] encodeBase64URLSafe(final byte[] binaryData)
From source file:org.wso2.carbon.identity.openidconnect.OpenIDConnectSystemClaimImpl.java
/** * This returns the base64url encoding of the left-most half of the hash of the octets of the ASCII representation * of the param value./*from w ww . j a v a 2 s . co m*/ * The hash algorithm used is the hash algorithm used in the alg Header Parameter of the ID Token's JOSE Header. * This method generate both c_hash and at_hash values when value is given as authorization code and access token * respectively. * @param value * @return at_hash or c_hash value * @throws IdentityOAuth2Exception */ private String getHashValue(String value) throws IdentityOAuth2Exception { String digAlg = OAuth2Util.mapDigestAlgorithm(signatureAlgorithm); MessageDigest md; try { md = MessageDigest.getInstance(digAlg); } catch (NoSuchAlgorithmException e) { throw new IdentityOAuth2Exception("Error creating the hash value. Invalid Digest Algorithm: " + digAlg); } md.update(value.getBytes(Charsets.UTF_8)); byte[] digest = md.digest(); int leftHalfBytes = 16; if (SHA384.equals(digAlg)) { leftHalfBytes = 24; } else if (SHA512.equals(digAlg)) { leftHalfBytes = 32; } byte[] leftmost = new byte[leftHalfBytes]; System.arraycopy(digest, 0, leftmost, 0, leftHalfBytes); return new String(Base64.encodeBase64URLSafe(leftmost), Charsets.UTF_8); }
From source file:org.wso2.identity.scenarios.access.delegation.oauth2.code.pkce.OAuth2AuthorizationCodeGrantWithPKCES256Test.java
/** * Generates PKCE code challenge./*from w ww . j ava 2s. co m*/ * * @param codeVerifier PKCE code verifier. * @return Code challenge. * @throws NoSuchAlgorithmException No Such Algorithm Exception. */ private String getPKCECodeChallenge(String codeVerifier) throws NoSuchAlgorithmException { MessageDigest digest = MessageDigest.getInstance("SHA-256"); byte[] hash = digest.digest(codeVerifier.getBytes(StandardCharsets.US_ASCII)); //Base64 encoded string is trimmed to remove trailing CR LF return new String(Base64.encodeBase64URLSafe(hash), StandardCharsets.UTF_8).trim(); }
From source file:services.plugins.atlassian.jira.jira1.client.JiraService.java
/** * Get the authentication digest for a request. * /*from w w w. ja va 2 s . co m*/ * The authentication is based on a hash:<br/> * Base64(SHA256([secret key]+"#" + requestUri + "#" + timestamp)) * * @param timestamp * the timestamp * @param requestUri * the request URI */ private String getAuthenticationDigest(long timestamp, String requestUri) throws JiraServiceException { try { MessageDigest digest = MessageDigest.getInstance("SHA-256"); String clearHash = this.key + "#" + requestUri + "#" + timestamp; return new String(Base64.encodeBase64URLSafe(digest.digest(clearHash.getBytes()))); } catch (NoSuchAlgorithmException e) { throw new JiraServiceException( "JiraService/getAuthenticationDigest: error when creating the authentication digest", e); } }