Search in sources :

Example 16 with MessageDigest

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

the class VersionInfoMojo method computeMD5.

private byte[] computeMD5(List<File> files) throws IOException, NoSuchAlgorithmException {
    MessageDigest md5 = MessageDigest.getInstance("MD5");
    for (File file : files) {
        getLog().debug("Computing MD5 for: " + file);
        md5.update(readFile(file));
    }
    return md5.digest();
}
Also used : MessageDigest(java.security.MessageDigest) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File)

Example 17 with MessageDigest

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

the class TestHBaseStore method hashSd.

@Test
public void hashSd() throws Exception {
    List<FieldSchema> cols = new ArrayList<FieldSchema>();
    cols.add(new FieldSchema("col1", "int", ""));
    SerDeInfo serde = new SerDeInfo("serde", "seriallib", null);
    StorageDescriptor sd = new StorageDescriptor(cols, "file:/tmp", "input", "output", true, 0, serde, null, null, emptyParameters);
    Map<List<String>, String> map = new HashMap<List<String>, String>();
    map.put(Arrays.asList("col3"), "col4");
    SkewedInfo skew = new SkewedInfo(Arrays.asList("col1"), Arrays.asList(Arrays.asList("col2")), map);
    sd.setSkewedInfo(skew);
    MessageDigest md = MessageDigest.getInstance("MD5");
    byte[] baseHash = HBaseUtils.hashStorageDescriptor(sd, md);
    StorageDescriptor changeSchema = new StorageDescriptor(sd);
    changeSchema.getCols().add(new FieldSchema("col2", "varchar(32)", "a comment"));
    byte[] schemaHash = HBaseUtils.hashStorageDescriptor(changeSchema, md);
    Assert.assertFalse(Arrays.equals(baseHash, schemaHash));
    StorageDescriptor changeLocation = new StorageDescriptor(sd);
    changeLocation.setLocation("file:/somewhere/else");
    byte[] locationHash = HBaseUtils.hashStorageDescriptor(changeLocation, md);
    Assert.assertArrayEquals(baseHash, locationHash);
}
Also used : SkewedInfo(org.apache.hadoop.hive.metastore.api.SkewedInfo) HashMap(java.util.HashMap) FieldSchema(org.apache.hadoop.hive.metastore.api.FieldSchema) SerDeInfo(org.apache.hadoop.hive.metastore.api.SerDeInfo) ArrayList(java.util.ArrayList) StorageDescriptor(org.apache.hadoop.hive.metastore.api.StorageDescriptor) List(java.util.List) ArrayList(java.util.ArrayList) MessageDigest(java.security.MessageDigest) Test(org.junit.Test)

Example 18 with MessageDigest

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

the class AuthUtilsTest method makeDigestPayloadTest.

@Test
public void makeDigestPayloadTest() throws NoSuchAlgorithmException {
    String section = "user-pass-section";
    Map<String, String> optionMap = new HashMap<String, String>();
    String user = "user";
    String pass = "pass";
    optionMap.put("username", user);
    optionMap.put("password", pass);
    AppConfigurationEntry entry = Mockito.mock(AppConfigurationEntry.class);
    Mockito.<Map<String, ?>>when(entry.getOptions()).thenReturn(optionMap);
    Configuration mockConfig = Mockito.mock(Configuration.class);
    Mockito.when(mockConfig.getAppConfigurationEntry(section)).thenReturn(new AppConfigurationEntry[] { entry });
    MessageDigest digest = MessageDigest.getInstance("SHA-512");
    byte[] output = digest.digest((user + ":" + pass).getBytes());
    String sha = Hex.encodeHexString(output);
    // previous code used this method to generate the string, ensure the two match
    StringBuilder builder = new StringBuilder();
    for (byte b : output) {
        builder.append(String.format("%02x", b));
    }
    String stringFormatMethod = builder.toString();
    Assert.assertEquals(AuthUtils.makeDigestPayload(mockConfig, "user-pass-section"), sha);
    Assert.assertEquals(sha, stringFormatMethod);
}
Also used : AppConfigurationEntry(javax.security.auth.login.AppConfigurationEntry) Configuration(javax.security.auth.login.Configuration) HashMap(java.util.HashMap) MessageDigest(java.security.MessageDigest) Test(org.junit.Test)

Example 19 with MessageDigest

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

the class ConcurrentMessageDigest method init.

/**
     * Ensures that {@link #digest(String, byte[][])} will support the specified
     * algorithm. This method <b>must</b> be called and return successfully
     * before using {@link #digest(String, byte[][])}.
     *
     * @param algorithm The message digest algorithm to be supported
     *
     * @throws NoSuchAlgorithmException If the algorithm is not supported by the
     *                                  JVM
     */
public static void init(String algorithm) throws NoSuchAlgorithmException {
    synchronized (queues) {
        if (!queues.containsKey(algorithm)) {
            MessageDigest md = MessageDigest.getInstance(algorithm);
            Queue<MessageDigest> queue = new ConcurrentLinkedQueue<>();
            queue.add(md);
            queues.put(algorithm, queue);
        }
    }
}
Also used : MessageDigest(java.security.MessageDigest) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue)

Example 20 with MessageDigest

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

the class ConcurrentMessageDigest method digest.

public static byte[] digest(String algorithm, int rounds, byte[]... input) {
    Queue<MessageDigest> queue = queues.get(algorithm);
    if (queue == null) {
        throw new IllegalStateException("Must call init() first");
    }
    MessageDigest md = queue.poll();
    if (md == null) {
        try {
            md = MessageDigest.getInstance(algorithm);
        } catch (NoSuchAlgorithmException e) {
            // first.
            throw new IllegalStateException("Must call init() first");
        }
    }
    // Round 1
    for (byte[] bytes : input) {
        md.update(bytes);
    }
    byte[] result = md.digest();
    // Subsequent rounds
    if (rounds > 1) {
        for (int i = 1; i < rounds; i++) {
            md.update(result);
            result = md.digest();
        }
    }
    queue.add(md);
    return result;
}
Also used : NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) MessageDigest(java.security.MessageDigest)

Aggregations

MessageDigest (java.security.MessageDigest)1122 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)562 IOException (java.io.IOException)161 UnsupportedEncodingException (java.io.UnsupportedEncodingException)93 BigInteger (java.math.BigInteger)93 FileInputStream (java.io.FileInputStream)68 InputStream (java.io.InputStream)66 File (java.io.File)58 DigestInputStream (java.security.DigestInputStream)56 Test (org.junit.Test)55 ByteArrayOutputStream (java.io.ByteArrayOutputStream)46 DigestOutputStream (java.security.DigestOutputStream)43 ArrayList (java.util.ArrayList)32 ByteArrayInputStream (java.io.ByteArrayInputStream)29 OutputStream (java.io.OutputStream)27 X509Certificate (java.security.cert.X509Certificate)27 GeneralSecurityException (java.security.GeneralSecurityException)25 Cipher (javax.crypto.Cipher)23 Provider (java.security.Provider)22 SecretKeySpec (javax.crypto.spec.SecretKeySpec)22