Example usage for java.security MessageDigest getInstance

List of usage examples for java.security MessageDigest getInstance

Introduction

In this page you can find the example usage for java.security MessageDigest getInstance.

Prototype

public static MessageDigest getInstance(String algorithm) throws NoSuchAlgorithmException 

Source Link

Document

Returns a MessageDigest object that implements the specified digest algorithm.

Usage

From source file:com.corngo.base.support.utils.security.Digests.java

/**
 * , ?md5sha1./*from w  w w .j a v a  2 s .  co m*/
 */
private static byte[] digest(byte[] input, String algorithm, byte[] salt, int iterations) {
    try {
        MessageDigest digest = MessageDigest.getInstance(algorithm);

        if (salt != null) {
            digest.update(salt);
        }

        byte[] result = digest.digest(input);

        for (int i = 1; i < iterations; i++) {
            digest.reset();
            result = digest.digest(result);
        }
        return result;
    } catch (GeneralSecurityException e) {
        e.printStackTrace();
        return null;
    }
}

From source file:cc.recommenders.utils.Fingerprints.java

private static MessageDigest createMessageDigest() throws NoSuchAlgorithmException {
    final MessageDigest digest = MessageDigest.getInstance(DIGEST_SHA1);
    return digest;
}

From source file:baldrickv.s3streamingtool.Hash.java

public static String hash(String algo, int output_bits, byte b[], int offset, int size) {
    try {/*from   ww  w  .j a  v  a 2s .  c  o  m*/
        int output_bytes = output_bits / 4; //hex = 4 bits per byte
        MessageDigest sig = MessageDigest.getInstance(algo);
        sig.update(b, offset, size);
        byte d[] = sig.digest();

        StringBuffer s = new StringBuffer(output_bytes);
        BigInteger bi = new BigInteger(1, d);
        s.append(bi.toString(16));
        while (s.length() < output_bytes) {
            s.insert(0, '0');
        }
        return s.toString();
    } catch (Exception e) {
        e.printStackTrace();
        System.exit(-1);
        return null;
    }

}

From source file:dualcontrol.DualControlDigest.java

public static byte[] digest(char[] chars) throws Exception {
    byte[] bytes = getBytes(chars);
    byte[] digestBytes = MessageDigest.getInstance(DIGEST_ALG).digest(bytes);
    Arrays.fill(bytes, (byte) 0);
    return digestBytes;
}

From source file:de.innovationgate.utils.UIDGenerator.java

/**
 * Generates an MD5 hash from the given string
 * @param hashString//from   w ww.  ja  v  a 2s  .  c o  m
 */
public static String generateMD5Hash(String hashString) {
    MessageDigest md5;
    try {
        md5 = MessageDigest.getInstance("MD5");
    } catch (NoSuchAlgorithmException e) {
        return null;
    }

    md5.update(hashString.getBytes());

    byte[] array = md5.digest();
    StringBuffer sb = new StringBuffer();
    for (int j = 0; j < array.length; ++j) {
        int b = array[j] & 0xFF;
        if (b < 0x10)
            sb.append('0');
        sb.append(Integer.toHexString(b));
    }
    return sb.toString();
}

From source file:com.sg.domail.entity.SiteAccount.java

public SiteAccount(String email, String password) {
    try {/*from w  w w  .  j a  v  a2s  .c  om*/
        this.md = MessageDigest.getInstance("MD5");
    } catch (NoSuchAlgorithmException ex) {
        throw new PlatformDoNotSupportMd5AlgorithmException(ex);
    }
    if (email == null || password == null || email.isEmpty() || password.isEmpty()) {
        throw new IllegalArgumentException();
    }
    this.email = email;
    this.passwordHash = getPasswordHash(password);
    this.emailVerified = false;
}

From source file:net.blogracy.model.hashes.Hashes.java

public static String hash(String text) throws NoSuchAlgorithmException {
    MessageDigest digester = MessageDigest.getInstance("SHA-1");
    byte[] digest = digester.digest(text.getBytes());
    String result = base32.encodeAsString(digest);
    return result;
}

From source file:org.nebulaframework.util.hashing.SHA1Generator.java

/**
 * Generates SHA1 Hash for the given {@code byte[]} and returns
 * the hash code as a {@code byte[]}.//w w w  .j  a  v a2s . co m
 * 
 * @param source source value
 * @return SHA-1 hash for value
 * 
 * @see #generate(String)
 * @see #generateAsString(byte[])
 * 
 * @throws IllegalArgumentException if {@code source} is {@code null}
 */
public static byte[] generate(byte[] source) throws IllegalArgumentException {

    // Check for nulls
    Assert.notNull(source);

    try {
        MessageDigest digest = MessageDigest.getInstance("SHA");
        digest.update(source);
        return digest.digest();
    } catch (NoSuchAlgorithmException e) {
        log.fatal("Cannot load hashing algorithm", e);
        throw new RuntimeException(e);
    }
}

From source file:edu.harvard.iq.dataverse.PasswordEncryption.java

public synchronized String encrypt(String plaintext) {
    MessageDigest md = null;//from   w ww  .j  a  v  a 2s .  co  m
    try {
        md = MessageDigest.getInstance("SHA");
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException(e);
    }
    try {
        md.update(plaintext.getBytes("UTF-8"));
    } catch (UnsupportedEncodingException e) {
        throw new RuntimeException(e);
    }
    byte[] raw = md.digest();
    String hash = Base64.encodeToString(raw, true);
    return hash;
}

From source file:Main.java

public static String encodeWifKey(boolean isPublicKeyCompressed, byte[] secret) {
    try {/*ww  w . j  a  va  2s .com*/
        MessageDigest digestSha = MessageDigest.getInstance("SHA-256");
        byte[] rawPrivateKey = new byte[isPublicKeyCompressed ? 38 : 37];
        rawPrivateKey[0] = (byte) 0x80;
        if (isPublicKeyCompressed) {
            rawPrivateKey[rawPrivateKey.length - 5] = 1;
        }
        System.arraycopy(secret, 0, rawPrivateKey, 1, secret.length);
        digestSha.update(rawPrivateKey, 0, rawPrivateKey.length - 4);
        byte[] check = digestSha.digest(digestSha.digest());
        System.arraycopy(check, 0, rawPrivateKey, rawPrivateKey.length - 4, 4);
        return encodeBase58(rawPrivateKey);
    } catch (NoSuchAlgorithmException e) {
        return null;
    }
}