Search in sources :

Example 81 with MessageDigest

use of java.security.MessageDigest in project platformlayer by platformlayer.

the class CryptoUtils method sha256.

public static Sha256Hash sha256(byte[]... data) {
    MessageDigest digest = getSha256();
    for (int i = 0; i < data.length - 1; i++) {
        digest.update(data[i]);
    }
    byte[] hash = digest.digest(data[data.length - 1]);
    return new Sha256Hash(hash);
}
Also used : MessageDigest(java.security.MessageDigest)

Example 82 with MessageDigest

use of java.security.MessageDigest in project cassandra by apache.

the class CacheProviderTest method assertDigests.

private void assertDigests(IRowCacheEntry one, CachedBTreePartition two) {
    assertTrue(one instanceof CachedBTreePartition);
    try {
        MessageDigest d1 = MessageDigest.getInstance("MD5");
        MessageDigest d2 = MessageDigest.getInstance("MD5");
        UnfilteredRowIterators.digest(((CachedBTreePartition) one).unfilteredIterator(), d1, MessagingService.current_version);
        UnfilteredRowIterators.digest(((CachedBTreePartition) two).unfilteredIterator(), d2, MessagingService.current_version);
        assertTrue(MessageDigest.isEqual(d1.digest(), d2.digest()));
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException(e);
    }
}
Also used : CachedBTreePartition(org.apache.cassandra.db.partitions.CachedBTreePartition) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) MessageDigest(java.security.MessageDigest)

Example 83 with MessageDigest

use of java.security.MessageDigest in project cassandra by apache.

the class UUIDGen method hash.

private static byte[] hash(Collection<InetAddress> data) {
    try {
        // Identify the host.
        MessageDigest messageDigest = MessageDigest.getInstance("MD5");
        for (InetAddress addr : data) messageDigest.update(addr.getAddress());
        // Identify the process on the load: we use both the PID and class loader hash.
        long pid = CLibrary.getProcessID();
        if (pid < 0)
            pid = new Random(System.currentTimeMillis()).nextLong();
        FBUtilities.updateWithLong(messageDigest, pid);
        ClassLoader loader = UUIDGen.class.getClassLoader();
        int loaderId = loader != null ? System.identityHashCode(loader) : 0;
        FBUtilities.updateWithInt(messageDigest, loaderId);
        return messageDigest.digest();
    } catch (NoSuchAlgorithmException nsae) {
        throw new RuntimeException("MD5 digest algorithm is not available", nsae);
    }
}
Also used : SecureRandom(java.security.SecureRandom) Random(java.util.Random) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) MessageDigest(java.security.MessageDigest) InetAddress(java.net.InetAddress)

Example 84 with MessageDigest

use of java.security.MessageDigest in project cassandra by apache.

the class PartitionTest method testDigest.

public void testDigest(int version) throws NoSuchAlgorithmException {
    ColumnFamilyStore cfs = Keyspace.open(KEYSPACE1).getColumnFamilyStore(CF_TENCOL);
    try {
        RowUpdateBuilder builder = new RowUpdateBuilder(cfs.metadata(), 5, "key1").clustering("c").add("val", "val1");
        for (int i = 0; i < 10; i++) builder.add("val" + i, "val" + i);
        builder.build().applyUnsafe();
        new RowUpdateBuilder(cfs.metadata(), 5, "key2").clustering("c").add("val", "val2").build().applyUnsafe();
        ReadCommand cmd1 = Util.cmd(cfs, "key1").build();
        ReadCommand cmd2 = Util.cmd(cfs, "key2").build();
        ImmutableBTreePartition p1 = Util.getOnlyPartitionUnfiltered(cmd1);
        ImmutableBTreePartition p2 = Util.getOnlyPartitionUnfiltered(cmd2);
        MessageDigest digest1 = MessageDigest.getInstance("MD5");
        MessageDigest digest2 = MessageDigest.getInstance("MD5");
        UnfilteredRowIterators.digest(p1.unfilteredIterator(), digest1, version);
        UnfilteredRowIterators.digest(p2.unfilteredIterator(), digest2, version);
        assertFalse(Arrays.equals(digest1.digest(), digest2.digest()));
        p1 = Util.getOnlyPartitionUnfiltered(Util.cmd(cfs, "key2").build());
        p2 = Util.getOnlyPartitionUnfiltered(Util.cmd(cfs, "key2").build());
        digest1 = MessageDigest.getInstance("MD5");
        digest2 = MessageDigest.getInstance("MD5");
        UnfilteredRowIterators.digest(p1.unfilteredIterator(), digest1, version);
        UnfilteredRowIterators.digest(p2.unfilteredIterator(), digest2, version);
        assertTrue(Arrays.equals(digest1.digest(), digest2.digest()));
        p1 = Util.getOnlyPartitionUnfiltered(Util.cmd(cfs, "key2").build());
        RowUpdateBuilder.deleteRow(cfs.metadata(), 6, "key2", "c").applyUnsafe();
        p2 = Util.getOnlyPartitionUnfiltered(Util.cmd(cfs, "key2").build());
        digest1 = MessageDigest.getInstance("MD5");
        digest2 = MessageDigest.getInstance("MD5");
        UnfilteredRowIterators.digest(p1.unfilteredIterator(), digest1, version);
        UnfilteredRowIterators.digest(p2.unfilteredIterator(), digest2, version);
        assertFalse(Arrays.equals(digest1.digest(), digest2.digest()));
    } finally {
        cfs.truncateBlocking();
    }
}
Also used : MessageDigest(java.security.MessageDigest)

Example 85 with MessageDigest

use of java.security.MessageDigest in project cassandra by apache.

the class ReadResponse method makeDigest.

protected static ByteBuffer makeDigest(UnfilteredPartitionIterator iterator, ReadCommand command) {
    MessageDigest digest = FBUtilities.threadLocalMD5Digest();
    UnfilteredPartitionIterators.digest(iterator, digest, command.digestVersion());
    return ByteBuffer.wrap(digest.digest());
}
Also used : MessageDigest(java.security.MessageDigest)

Aggregations

MessageDigest (java.security.MessageDigest)1237 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)613 IOException (java.io.IOException)176 UnsupportedEncodingException (java.io.UnsupportedEncodingException)102 BigInteger (java.math.BigInteger)101 InputStream (java.io.InputStream)72 FileInputStream (java.io.FileInputStream)70 File (java.io.File)62 DigestInputStream (java.security.DigestInputStream)61 Test (org.junit.Test)61 ByteArrayOutputStream (java.io.ByteArrayOutputStream)51 DigestOutputStream (java.security.DigestOutputStream)45 ArrayList (java.util.ArrayList)37 ByteArrayInputStream (java.io.ByteArrayInputStream)31 X509Certificate (java.security.cert.X509Certificate)29 OutputStream (java.io.OutputStream)28 GeneralSecurityException (java.security.GeneralSecurityException)25 Cipher (javax.crypto.Cipher)25 SecretKeySpec (javax.crypto.spec.SecretKeySpec)25 Provider (java.security.Provider)22