List of usage examples for com.google.common.hash HashingOutputStream hash
@CheckReturnValue
public HashCode hash()
From source file:com.google.api.services.samples.storage.cmdline.StorageSample.java
public static void main(String[] args) { try {/*ww w .j av a 2 s. co m*/ // initialize network, sample settings, credentials, and the storage client. HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport(); JsonFactory jsonFactory = JacksonFactory.getDefaultInstance(); SampleSettings settings = SampleSettings.load(jsonFactory); Credential credential = CredentialsProvider.authorize(httpTransport, jsonFactory); Storage storage = new Storage.Builder(httpTransport, jsonFactory, credential) .setApplicationName(APPLICATION_NAME).build(); // // run commands // View.header1("Trying to create a new bucket " + settings.getBucket()); BucketsInsertExample.createInProject(storage, settings.getProject(), new Bucket().setName(settings.getBucket()).setLocation("US")); View.header1("Getting bucket " + settings.getBucket() + " metadata"); Bucket bucket = BucketsGetExample.get(storage, settings.getBucket()); View.show(bucket); View.header1("Listing objects in bucket " + settings.getBucket()); for (StorageObject object : ObjectsListExample.list(storage, settings.getBucket())) { View.show(object); } View.header1("Getting object metadata from gs://pub/SomeOfTheTeam.jpg"); StorageObject object = ObjectsGetMetadataExample.get(storage, "pub", "SomeOfTheTeam.jpg"); View.show(object); View.header1("Uploading object."); final long objectSize = 100 * 1024 * 1024 /* 100 MB */; InputStream data = new Helpers.RandomDataBlockInputStream(objectSize, 1024); object = new StorageObject().setBucket(settings.getBucket()).setName(settings.getPrefix() + "myobject") .setMetadata(ImmutableMap.of("key1", "value1", "key2", "value2")) .setCacheControl("max-age=3600, must-revalidate").setContentDisposition("attachment"); object = ObjectsUploadExample.uploadWithMetadata(storage, object, data); View.show(object); System.out.println("md5Hash: " + object.getMd5Hash()); System.out.println("crc32c: " + object.getCrc32c() + ", decoded to " + ByteBuffer.wrap(BaseEncoding.base64().decode(object.getCrc32c())).getInt()); View.header1("Getting object data of uploaded object, calculate hashes/crcs."); OutputStream nullOutputStream = new OutputStream() { // Throws away the bytes. @Override public void write(int b) throws IOException { } @Override public void write(byte b[], int off, int len) { } }; DigestOutputStream md5DigestOutputStream = new DigestOutputStream(nullOutputStream, MessageDigest.getInstance("MD5")); HashingOutputStream crc32cHashingOutputStream = new HashingOutputStream(Hashing.crc32c(), md5DigestOutputStream); ObjectsDownloadExample.downloadToOutputStream(storage, settings.getBucket(), settings.getPrefix() + "myobject", crc32cHashingOutputStream); String calculatedMD5 = BaseEncoding.base64().encode(md5DigestOutputStream.getMessageDigest().digest()); System.out.println( "md5Hash: " + calculatedMD5 + " " + (object.getMd5Hash().equals(calculatedMD5) ? "(MATCHES)" : "(MISMATCHES; data altered in transit)")); int calculatedCrc32c = crc32cHashingOutputStream.hash().asInt(); String calculatedEncodedCrc32c = BaseEncoding.base64().encode(Ints.toByteArray(calculatedCrc32c)); // NOTE: Don't compare HashCode.asBytes() directly, as that encodes the crc32c in // little-endien. One would have to reverse the bytes first. System.out.println("crc32c: " + calculatedEncodedCrc32c + ", decoded to " + crc32cHashingOutputStream.hash().asInt() + " " + (object.getCrc32c().equals(calculatedEncodedCrc32c) ? "(MATCHES)" : "(MISMATCHES; data altered in transit)")); // success! return; } catch (GoogleJsonResponseException e) { // An error came back from the API. GoogleJsonError error = e.getDetails(); System.err.println(error.getMessage()); // More error information can be retrieved with error.getErrors(). } catch (HttpResponseException e) { // No JSON body was returned by the API. System.err.println(e.getHeaders()); System.err.println(e.getMessage()); } catch (IOException e) { // Error formulating a HTTP request or reaching the HTTP service. System.err.println(e.getMessage()); } catch (Throwable t) { t.printStackTrace(); } System.exit(1); }
From source file:uk.ac.horizon.artcodes.server.christmas.ImageServlet.java
@Override public void doPut(HttpServletRequest request, HttpServletResponse response) throws IOException { try {/* w ww .ja v a 2 s .c o m*/ if (request.getContentLength() > image_size) { throw new HTTPException(HttpServletResponse.SC_REQUEST_ENTITY_TOO_LARGE, "Image too large"); } verifyApp(request); final String id = getImageID(request); final GcsService gcsService = GcsServiceFactory.createGcsService(RetryParams.getDefaultInstance()); final GcsFilename filename = new GcsFilename(request.getServerName(), id); final GcsFileMetadata metadata = gcsService.getMetadata(filename); if (metadata != null) { throw new HTTPException(HttpServletResponse.SC_FORBIDDEN, "Cannot modify"); } final BufferedInputStream inputStream = new BufferedInputStream(request.getInputStream()); final String mimetype = URLConnection.guessContentTypeFromStream(inputStream); if (mimetype == null) { throw new HTTPException(HttpServletResponse.SC_BAD_REQUEST, "Unrecognised image type"); } final GcsFileOptions.Builder fileOptionsBuilder = new GcsFileOptions.Builder(); fileOptionsBuilder.mimeType(mimetype); final GcsFileOptions fileOptions = fileOptionsBuilder.build(); final GcsOutputChannel outputChannel = gcsService.createOrReplace(filename, fileOptions); final HashingOutputStream outputStream = new HashingOutputStream(Hashing.sha256(), Channels.newOutputStream(outputChannel)); ByteStreams.copy(inputStream, outputStream); String hash = outputStream.hash().toString(); if (!hash.equals(id)) { gcsService.delete(filename); throw new HTTPException(HttpServletResponse.SC_BAD_REQUEST, "Invalid hash"); } outputStream.close(); outputChannel.close(); } catch (HTTPException e) { e.writeTo(response); } }
From source file:uk.ac.horizon.artcodes.server.ImageServlet.java
@Override public void doPut(HttpServletRequest request, HttpServletResponse response) throws IOException { try {// www .java 2s.com if (request.getContentLength() > image_size) { throw new HTTPException(HttpServletResponse.SC_REQUEST_ENTITY_TOO_LARGE, "Image too large"); } verifyUser(getUser(request)); final String id = getImageID(request); final GcsService gcsService = GcsServiceFactory.createGcsService(RetryParams.getDefaultInstance()); final GcsFilename filename = new GcsFilename(request.getServerName(), id); final GcsFileMetadata metadata = gcsService.getMetadata(filename); if (metadata != null) { throw new HTTPException(HttpServletResponse.SC_FORBIDDEN, "Cannot modify"); } final BufferedInputStream inputStream = new BufferedInputStream(request.getInputStream()); final String mimetype = URLConnection.guessContentTypeFromStream(inputStream); if (mimetype == null) { throw new HTTPException(HttpServletResponse.SC_BAD_REQUEST, "Unrecognised image type"); } final GcsFileOptions.Builder fileOptionsBuilder = new GcsFileOptions.Builder(); fileOptionsBuilder.mimeType(mimetype); final GcsFileOptions fileOptions = fileOptionsBuilder.build(); final GcsOutputChannel outputChannel = gcsService.createOrReplace(filename, fileOptions); final HashingOutputStream outputStream = new HashingOutputStream(Hashing.sha256(), Channels.newOutputStream(outputChannel)); ByteStreams.copy(inputStream, outputStream); String hash = outputStream.hash().toString(); if (!hash.equals(id)) { gcsService.delete(filename); throw new HTTPException(HttpServletResponse.SC_BAD_REQUEST, "Invalid hash"); } outputStream.close(); outputChannel.close(); } catch (HTTPException e) { e.writeTo(response); } }
From source file:com.google.devtools.build.lib.collect.nestedset.NestedSetCodec.java
private void serializeOneNestedSet(Object children, CodedOutputStream codedOut, Map<Object, byte[]> childToDigest) throws IOException, SerializationException { // Serialize nested set into an inner byte array so we can take its digest ByteArrayOutputStream childOutputStream = new ByteArrayOutputStream(); HashingOutputStream hashingOutputStream = new HashingOutputStream(Hashing.md5(), childOutputStream); CodedOutputStream childCodedOut = CodedOutputStream.newInstance(hashingOutputStream); if (children instanceof Object[]) { serializeMultiItemChildArray((Object[]) children, childToDigest, childCodedOut); } else if (children != NestedSet.EMPTY_CHILDREN) { serializeSingleItemChildArray(children, childCodedOut); } else {/*from w ww. j a va 2 s. c om*/ // Empty set childCodedOut.writeInt32NoTag(0); } childCodedOut.flush(); byte[] digest = hashingOutputStream.hash().asBytes(); codedOut.writeByteArrayNoTag(digest); byte[] childBytes = childOutputStream.toByteArray(); codedOut.writeByteArrayNoTag(childBytes); childToDigest.put(children, digest); }
From source file:io.macgyver.plugin.cmdb.AppInstanceManager.java
public String computeSignature(ObjectNode n, Set<String> exclusions) { List<String> list = Lists.newArrayList(n.fieldNames()); Collections.sort(list);//from w ww . ja v a2s.c om HashingOutputStream hos = new HashingOutputStream(Hashing.sha1(), ByteStreams.nullOutputStream()); list.forEach(it -> { if (exclusions != null && !exclusions.contains(it)) { JsonNode val = n.get(it); if (val.isObject() || val.isArray()) { // skipping } else { try { hos.write(it.getBytes()); hos.write(val.toString().getBytes()); } catch (IOException e) { } } } }); return hos.hash().toString(); }