public void testFromStringFailsWithShortInputs() { try { HashCode.fromString(""); fail(); } catch (IllegalArgumentException expected) { } try { HashCode.fromString("7"); fail(); } catch (IllegalArgumentException expected) { } HashCode unused = HashCode.fromString("7f"); }
public void testFromStringFailsWithOddLengthInput() { try { HashCode.fromString("7f8"); fail(); } catch (IllegalArgumentException expected) { } }
public void testFromStringFailsWithInvalidHexChar() { try { HashCode.fromString("7f8005ff0z"); fail(); } catch (IllegalArgumentException expected) { } }
private static void checkHmac(String expected, HashFunction hashFunc, byte[] data) { assertEquals(HashCode.fromString(expected), hashFunc.hashBytes(data)); }
@DELETE @Path("/v1/proxy") @Produces(APPLICATION_JSON) public void cancelQuery( @QueryParam("uri") String uri, @QueryParam("hmac") String hash, @Context HttpServletRequest servletRequest, @Suspended AsyncResponse asyncResponse) { if (!hmac.hashString(uri, UTF_8).equals(HashCode.fromString(hash))) { throw badRequest(FORBIDDEN, "Failed to validate HMAC of URI"); } Request.Builder request = prepareDelete().setUri(URI.create(uri)); performRequest(servletRequest, asyncResponse, request, response -> responseWithHeaders(noContent(), response)); }
@GET @Path("/v1/proxy") @Produces(APPLICATION_JSON) public void getNext( @QueryParam("uri") String uri, @QueryParam("hmac") String hash, @Context HttpServletRequest servletRequest, @Context UriInfo uriInfo, @Suspended AsyncResponse asyncResponse) { if (!hmac.hashString(uri, UTF_8).equals(HashCode.fromString(hash))) { throw badRequest(FORBIDDEN, "Failed to validate HMAC of URI"); } Request.Builder request = prepareGet().setUri(URI.create(uri)); performRequest(servletRequest, asyncResponse, request, response -> buildResponse(uriInfo, response)); }
public void testFromStringFailsWithUpperCaseString() { String string = Hashing.sha1().hashString("foo", Charsets.US_ASCII).toString().toUpperCase(); try { HashCode.fromString(string); fail(); } catch (IllegalArgumentException expected) { } }
public void testRoundTripHashCodeUsingFromString() { HashCode hash1 = Hashing.sha1().hashString("foo", Charsets.US_ASCII); HashCode hash2 = HashCode.fromString(hash1.toString()); assertEquals(hash1, hash2); }
@Override protected HashCode _deserialize(String value, DeserializationContext ctxt) throws IOException { return HashCode.fromString(value.toLowerCase(Locale.ENGLISH)); } }
@Override protected HashCode _deserialize(String value, DeserializationContext ctxt) throws IOException { return HashCode.fromString(value.toLowerCase(Locale.ENGLISH)); } }
public void testFromStringFailsWithShortInputs() { try { HashCode.fromString(""); fail(); } catch (IllegalArgumentException expected) { } try { HashCode.fromString("7"); fail(); } catch (IllegalArgumentException expected) { } HashCode unused = HashCode.fromString("7f"); }
/** * Extract checksums of asset blob if checksums are present in asset attributes. */ public Map<HashAlgorithm, HashCode> getChecksums(final Iterable<HashAlgorithm> hashAlgorithms) { final NestedAttributesMap checksumAttributes = attributes().child(CHECKSUM); final Map<HashAlgorithm, HashCode> hashCodes = Maps.newHashMap(); for (HashAlgorithm algorithm : hashAlgorithms) { final HashCode hashCode = HashCode.fromString(checksumAttributes.require(algorithm.name(), String.class)); hashCodes.put(algorithm, hashCode); } return hashCodes; }
public void testFromStringFailsWithInvalidHexChar() { try { HashCode.fromString("7f8005ff0z"); fail(); } catch (IllegalArgumentException expected) { } }
public void testFromStringFailsWithOddLengthInput() { try { HashCode.fromString("7f8"); fail(); } catch (IllegalArgumentException expected) { } }
/** * Extract checksum of asset blob if checksum is present in asset attributes. */ @Nullable public HashCode getChecksum(final HashAlgorithm hashAlgorithm) { String hashCode = attributes().child(CHECKSUM).get(hashAlgorithm.name(), String.class); if (hashCode != null) { return HashCode.fromString(hashCode); } return null; }
private static void checkHmac(String expected, HashFunction hashFunc, byte[] data) { assertEquals(HashCode.fromString(expected), hashFunc.hashBytes(data)); }
public void testFromStringFailsWithUpperCaseString() { String string = Hashing.sha1().hashString("foo", Charsets.US_ASCII).toString().toUpperCase(); try { HashCode.fromString(string); fail(); } catch (IllegalArgumentException expected) { } }
public void testRoundTripHashCodeUsingFromString() { HashCode hash1 = Hashing.sha1().hashString("foo", Charsets.US_ASCII); HashCode hash2 = HashCode.fromString(hash1.toString()); assertEquals(hash1, hash2); }
private Blob createBlob( FileResource fileResource, File file ) { return blobStore.blobBuilder( fileResource.getStorageKey() ) .payload( file ) .contentLength( fileResource.getContentLength() ) .contentMD5( HashCode.fromString( fileResource.getContentMd5() ) ) .contentType( fileResource.getContentType() ) .contentDisposition( "filename=" + fileResource.getName() ) .build(); }
private Blob createBlob( FileResource fileResource, byte[] bytes ) { return blobStore.blobBuilder( fileResource.getStorageKey() ) .payload( bytes ) .contentLength( fileResource.getContentLength() ) .contentMD5( HashCode.fromString( fileResource.getContentMd5() ) ) .contentType( fileResource.getContentType() ) .contentDisposition( "filename=" + fileResource.getName() ) .build(); }