Example usage for com.google.common.hash Hashing crc32c

List of usage examples for com.google.common.hash Hashing crc32c

Introduction

In this page you can find the example usage for com.google.common.hash Hashing crc32c.

Prototype

public static HashFunction crc32c() 

Source Link

Document

Returns a hash function implementing the CRC32C checksum algorithm (32 hash bits) as described by RFC 3720, Section 12.1.

Usage

From source file:com.google.api.services.samples.storage.cmdline.StorageSample.java

public static void main(String[] args) {
    try {/* ww w. j a v  a 2 s.  com*/
        // 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:org.graylog.plugins.pipelineprocessor.functions.hashing.CRC32C.java

@Override
protected String getDigest(String value) {
    return Hashing.crc32c().hashString(value, StandardCharsets.UTF_8).toString();
}

From source file:com.torodb.torod.mongodb.repl.ObjectIdFactory.java

private static int createMachineId() {
    int machineId;
    try {/*ww w.  jav  a2 s  .c  o  m*/
        Hasher hasher = Hashing.crc32c().newHasher();
        Enumeration<NetworkInterface> nics = NetworkInterface.getNetworkInterfaces();
        boolean atLeastOne = false;
        while (nics.hasMoreElements()) {
            NetworkInterface ni = nics.nextElement();
            if (ni != null) {
                byte[] macAddress = ni.getHardwareAddress();
                if (macAddress != null) {
                    for (byte _byte : macAddress) {
                        atLeastOne = true;
                        hasher.putByte(_byte);
                    }
                }
            }
        }
        if (!atLeastOne) {
            LOGGER.warn("Failed to calculate the machine id. A random number is used");
            machineId = new SecureRandom().nextInt();
        } else {
            machineId = hasher.hash().asInt();
        }
    } catch (SocketException ex) {
        LOGGER.warn("Failed to calculate the machine id. A random number is used");
        machineId = new SecureRandom().nextInt();
    }

    return machineId & 0xFFFFFF;
}

From source file:com.torodb.mongodb.language.ObjectIdFactory.java

private static int createMachineId() {
    int machineId;
    try {//from www  . j a v  a 2  s. c  o  m
        Hasher hasher = Hashing.crc32c().newHasher();
        Enumeration<NetworkInterface> nics = NetworkInterface.getNetworkInterfaces();
        boolean atLeastOne = false;
        while (nics.hasMoreElements()) {
            NetworkInterface ni = nics.nextElement();
            if (ni != null) {
                byte[] macAddress = ni.getHardwareAddress();
                if (macAddress != null) {
                    for (byte b : macAddress) {
                        atLeastOne = true;
                        hasher.putByte(b);
                    }
                }
            }
        }
        if (!atLeastOne) {
            LOGGER.warn("Failed to calculate the machine id. A random number is used");
            machineId = new SecureRandom().nextInt();
        } else {
            machineId = hasher.hash().asInt();
        }
    } catch (SocketException ex) {
        LOGGER.warn("Failed to calculate the machine id. A random number is used");
        machineId = new SecureRandom().nextInt();
    }

    return machineId & 0xFFFFFF;
}

From source file:ph.samson.maven.enforcer.rule.checksum.FileChecksum.java

@Override
public void execute(EnforcerRuleHelper erh) throws EnforcerRuleException {
    if (file == null || !file.canRead()) {
        throw new EnforcerRuleException("Missing file: " + file);
    }//from   w  ww  .  j a  v a 2  s .c om

    HashFunction hashFn;
    switch (type) {
    case "crc32":
        hashFn = Hashing.crc32();
        break;
    case "crc32c":
        hashFn = Hashing.crc32c();
        break;
    case "md5":
        hashFn = Hashing.md5();
        break;
    case "sha1":
        hashFn = Hashing.sha1();
        break;
    case "sha256":
        hashFn = Hashing.sha256();
        break;
    case "sha512":
        hashFn = Hashing.sha512();
        break;
    default:
        throw new EnforcerRuleException("Unsupported hash type: " + type);
    }

    String hash;
    try {
        hash = hashFn.hashBytes(Files.readAllBytes(file.toPath())).toString();
    } catch (IOException ex) {
        throw new EnforcerRuleException("Failed reading " + file, ex);
    }

    if (!hash.equalsIgnoreCase(checksum)) {
        throw new EnforcerRuleException(
                type + " hash of " + file + " was " + hash + " but expected " + checksum);
    }
}

From source file:org.lightjason.agentspeak.action.builtin.crypto.CHash.java

/**
 * runs hashing function with difference between Google Guava hashing and Java default digest
 *
 * @param p_context execution context/*from   w w  w. j  a va2s. c  o m*/
 * @param p_algorithm algorithm name
 * @param p_data byte data representation
 * @return hash value
 */
private static String hash(@Nonnull final IContext p_context, @Nonnull final String p_algorithm,
        @Nonnull final byte[] p_data) {
    switch (p_algorithm.trim().toLowerCase(Locale.ROOT)) {
    case "adler-32":
        return Hashing.adler32().newHasher().putBytes(p_data).hash().toString();

    case "crc-32":
        return Hashing.crc32().newHasher().putBytes(p_data).hash().toString();

    case "crc-32c":
        return Hashing.crc32c().newHasher().putBytes(p_data).hash().toString();

    case "murmur3-32":
        return Hashing.murmur3_32().newHasher().putBytes(p_data).hash().toString();

    case "murmur3-128":
        return Hashing.murmur3_128().newHasher().putBytes(p_data).hash().toString();

    case "siphash-2-4":
        return Hashing.sipHash24().newHasher().putBytes(p_data).hash().toString();

    default:
        try {
            return BaseEncoding.base16().encode(MessageDigest.getInstance(p_algorithm).digest(p_data))
                    .toLowerCase(Locale.ROOT);
        } catch (final NoSuchAlgorithmException l_exception) {
            throw new CRuntimeException(l_exception, p_context);
        }
    }
}

From source file:org.lightjason.agentspeak.action.buildin.crypto.CHash.java

/**
 * runs hashing function with difference between Google Guava hashing and Java default digest
 *
 * @param p_algorithm algorithm name//from w  ww.ja v a2s  . c o  m
 * @param p_data byte data representation
 * @return hash value
 *
 * @throws NoSuchAlgorithmException on unknown hashing algorithm
 */
private String hash(final String p_algorithm, final byte[] p_data) throws NoSuchAlgorithmException {
    switch (p_algorithm.trim().toLowerCase(Locale.ROOT)) {
    case "adler-32":
        return Hashing.adler32().newHasher().putBytes(p_data).hash().toString();

    case "crc-32":
        return Hashing.crc32().newHasher().putBytes(p_data).hash().toString();

    case "crc-32c":
        return Hashing.crc32c().newHasher().putBytes(p_data).hash().toString();

    case "murmur3-32":
        return Hashing.murmur3_32().newHasher().putBytes(p_data).hash().toString();

    case "murmur3-128":
        return Hashing.murmur3_128().newHasher().putBytes(p_data).hash().toString();

    case "siphash-2-4":
        return Hashing.sipHash24().newHasher().putBytes(p_data).hash().toString();

    default:
        return String.format("%032x", new BigInteger(1, MessageDigest.getInstance(p_algorithm).digest(p_data)));
    }
}

From source file:com.google.cloud.hadoop.gcsio.testing.InMemoryObjectEntry.java

public InMemoryObjectEntry(String bucketName, String objectName, long createTimeMillis, String contentType,
        Map<String, byte[]> metadata) {
    // Override close() to commit its completed byte array into completedContents to reflect
    // the behavior that any readable contents are only well-defined if the writeStream is closed.
    writeStream = new ByteArrayOutputStream() {
        @Override/*from w ww  .j  a  v a 2 s. co  m*/
        public synchronized void close() throws IOException {
            synchronized (InMemoryObjectEntry.this) {
                completedContents = toByteArray();
                HashCode md5 = Hashing.md5().hashBytes(completedContents);
                HashCode crc32c = Hashing.crc32c().hashBytes(completedContents);
                writeStream = null;
                writeChannel = null;
                info = new GoogleCloudStorageItemInfo(info.getResourceId(), info.getCreationTime(),
                        completedContents.length, null, null, info.getContentType(), info.getMetadata(),
                        info.getContentGeneration(), 0L,
                        new VerificationAttributes(md5.asBytes(), Ints.toByteArray(crc32c.asInt())));
            }
        }
    };

    // We have to delegate because we can't extend from the inner class returned by,
    // Channels.newChannel, and the default version doesn't enforce ClosedChannelException
    // when trying to write to a closed channel; probably because it relies on the underlying
    // output stream throwing when closed. The ByteArrayOutputStream doesn't enforce throwing
    // when closed, so we have to manually do it.
    WritableByteChannel delegateWriteChannel = Channels.newChannel(writeStream);
    writeChannel = new WritableByteChannelImpl(delegateWriteChannel);

    // Size 0 initially because this object exists, but contains no data.
    info = new GoogleCloudStorageItemInfo(new StorageResourceId(bucketName, objectName), createTimeMillis, 0,
            null, null, contentType, ImmutableMap.copyOf(metadata), createTimeMillis, 0L);
}

From source file:com.google.gcloud.storage.StorageImpl.java

@Override
public Blob create(BlobInfo blobInfo, byte[] content, BlobTargetOption... options) {
    content = firstNonNull(content, EMPTY_BYTE_ARRAY);
    BlobInfo updatedInfo = blobInfo.toBuilder()
            .md5(BaseEncoding.base64().encode(Hashing.md5().hashBytes(content).asBytes()))
            .crc32c(BaseEncoding.base64().encode(Ints.toByteArray(Hashing.crc32c().hashBytes(content).asInt())))
            .build();/* w ww.ja  va2  s . c  o m*/
    return create(updatedInfo, new ByteArrayInputStream(content), options);
}

From source file:com.google.cloud.storage.StorageImpl.java

@Override
public Blob create(BlobInfo blobInfo, byte[] content, BlobTargetOption... options) {
    content = firstNonNull(content, EMPTY_BYTE_ARRAY);
    BlobInfo updatedInfo = blobInfo.toBuilder()
            .setMd5(BaseEncoding.base64().encode(Hashing.md5().hashBytes(content).asBytes()))
            .setCrc32c(/*from   w  w w .jav a 2 s.  c o  m*/
                    BaseEncoding.base64().encode(Ints.toByteArray(Hashing.crc32c().hashBytes(content).asInt())))
            .build();
    return internalCreate(updatedInfo, content, options);
}