@SuppressWarnings("deprecation") private static byte[] computeGuid(byte[] bytes) { return DigestUtils.sha(bytes); }
/** * @param data for hashing * @return sha 1 hash of specified data */ public static byte[] calculateSha1Hash(byte[] data) { return DigestUtils.sha(data); }
/** * @return a unique string identifier for this {@link DatasetAndPartition}. */ @SuppressWarnings("deprecation") public String identifier() { return Hex.encodeHexString(DigestUtils.sha(this.dataset.toString() + this.partition)); } }
public String generateOAuthSecretKey( AuthPrincipalType type ) { long timestamp = System.currentTimeMillis(); ByteBuffer bytes = ByteBuffer.allocate( 20 ); bytes.put( sha( timestamp + OAUTH_SECRET_SALT + UUID.randomUUID() ) ); return type.getBase64Prefix() + encodeBase64URLSafeString( bytes.array() ); }
private UUID getUUIDForToken(String token ) throws ExpiredTokenException, BadTokenException { TokenCategory tokenCategory = TokenCategory.getFromBase64String( token ); byte[] bytes = decodeBase64( token.substring( TokenCategory.BASE64_PREFIX_LENGTH ) ); UUID uuid = uuid( bytes ); int i = 16; long expires = Long.MAX_VALUE; if ( tokenCategory.getExpires() ) { expires = ByteBuffer.wrap( bytes, i, 8 ).getLong(); i = 24; } ByteBuffer expected = ByteBuffer.allocate( 20 ); expected.put( sha( tokenCategory.getPrefix() + uuid + tokenSecretSalt + expires ) ); expected.rewind(); ByteBuffer signature = ByteBuffer.wrap( bytes, i, 20 ); if ( !signature.equals( expected ) ) { throw new BadTokenException( "Invalid token signature" ); } long expirationDelta = System.currentTimeMillis() - expires; if ( expires != Long.MAX_VALUE && expirationDelta > 0 ) { throw new ExpiredTokenException( String.format( "Token expired %d milliseconds ago.", expirationDelta ) ); } return uuid; }
private String getTokenForUUID( TokenInfo tokenInfo, TokenCategory tokenCategory, UUID uuid ) { int l = 36; if ( tokenCategory.getExpires() ) { l += 8; } ByteBuffer bytes = ByteBuffer.allocate( l ); bytes.put( bytes( uuid ) ); long expires = Long.MAX_VALUE; if ( tokenCategory.getExpires() ) { expires = ( tokenInfo.getDuration() > 0 ) ? UUIDUtils.getTimestampInMillis( uuid ) + ( tokenInfo.getDuration() ) : UUIDUtils.getTimestampInMillis( uuid ) + getExpirationForTokenType( tokenCategory ); bytes.putLong( expires ); } bytes.put( sha( tokenCategory.getPrefix() + uuid + tokenSecretSalt + expires ) ); return tokenCategory.getBase64Prefix() + encodeBase64URLSafeString( bytes.array() ); }
/** * Calculates the SHA digest and returns the value as a * <code>byte[]</code>. * * @param data Data to digest * @return SHA digest */ public static byte[] sha(String data) { return sha(data.getBytes()); }
/** * Calculates the SHA digest and returns the value as a hex string. * * @param data Data to digest * @return SHA digest as a hex string */ public static String shaHex(String data) { return new String(Hex.encodeHex(sha(data))); }
/** * Calculates the SHA digest and returns the value as a hex string. * * @param data Data to digest * @return SHA digest as a hex string */ public static String shaHex(byte[] data) { return new String(Hex.encodeHex(sha(data))); }
private static String hash(String string) { byte[] shaArray = DigestUtils.sha(string); byte[] encodedArray = Base64.getEncoder().encode(shaArray); String returnValue = new String(encodedArray); returnValue = StringUtils.removeEnd(returnValue, "\r\n"); return returnValue.replaceAll("=", "").replaceAll("/", "-").replaceAll("\\+", "\\_"); }
/** * @return a unique string identifier for this {@link DatasetAndPartition}. */ @SuppressWarnings("deprecation") public String identifier() { return Hex.encodeHexString(DigestUtils.sha(this.dataset.toString() + this.partition)); } }
/** * @return a unique string identifier for this {@link DatasetAndPartition}. */ @SuppressWarnings("deprecation") public String identifier() { return Hex.encodeHexString(DigestUtils.sha(this.dataset.toString() + this.partition)); } }
public String getLockedDomainPrefix(Uri gadgetUri) { byte[] sha1 = DigestUtils.sha(gadgetUri.toString()); return new String(Base32.encodeBase32(sha1)); // a hash } }
public String getLockedDomainPrefix(Uri gadgetUri) { byte[] sha1 = DigestUtils.sha(gadgetUri.toString()); return new String(Base32.encodeBase32(sha1)); // a hash } }
private String getLockedDomain(Gadget gadget, String container) { String suffix = lockedSuffixes.get(container); if (suffix == null) { return null; } byte[] sha1 = DigestUtils.sha(gadget.getSpec().getUrl().toString()); String hash = new String(Base32.encodeBase32(sha1)); return hash + suffix; }
private String getLockedDomain(Gadget gadget, String container) { String suffix = lockedSuffixes.get(container); if (suffix == null) { return null; } byte[] sha1 = DigestUtils.sha(gadget.getSpec().getUrl().toString()); String hash = new String(Base32.encodeBase32(sha1)); return hash + suffix; }
@Test public void testBodyHashNoContentType() throws Exception { FakeHttpServletRequest req = new FakeHttpServletRequest(); req.setPostData(CharsetUtil.getUtf8Bytes("")); String hash = new String(Base64.encodeBase64(DigestUtils.sha(CharsetUtil .getUtf8Bytes(""))), "UTF-8"); OAuthAuthenticationHandler.verifyBodyHash(req, hash); } }
@Test public void testBodyHashNoContentType() throws Exception { FakeHttpServletRequest req = new FakeHttpServletRequest(); req.setPostData(CharsetUtil.getUtf8Bytes("")); String hash = new String(Base64.encodeBase64(DigestUtils.sha(CharsetUtil .getUtf8Bytes(""))), "UTF-8"); OAuthAuthenticationHandler.verifyBodyHash(req, hash); } }
@Test public void testBodyHashNoContentType() throws Exception { FakeHttpServletRequest req = new FakeHttpServletRequest(); req.setPostData(CharsetUtil.getUtf8Bytes("")); String hash = new String(Base64.encodeBase64(DigestUtils.sha(CharsetUtil.getUtf8Bytes(""))), "UTF-8"); OAuthAuthenticationHandler.verifyBodyHash(req, hash); } }
@Test public void testBodySigning() throws Exception { FakeHttpServletRequest req = new FakeHttpServletRequest(); req.setContentType("text/plain"); String body = "BODY"; req.setPostData(CharsetUtil.getUtf8Bytes(body)); String hash = new String(Base64.encodeBase64(DigestUtils.sha(CharsetUtil.getUtf8Bytes(body))), "UTF-8"); req.setParameter(OAuthConstants.OAUTH_BODY_HASH, hash); OAuthAuthenticationHandler.verifyBodyHash(req, hash); }