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:Main.java

/**
 * Generates SHA256 hash of the password which is used as key
 *
 * @param password used to generated key
 * @return SHA256 of the password/*from  w  ww  .j ava 2s .co m*/
 */
private static SecretKeySpec generateKey(final String password)
        throws NoSuchAlgorithmException, UnsupportedEncodingException {
    final MessageDigest digest = MessageDigest.getInstance(HASH_ALGORITHM);
    byte[] bytes = password.getBytes("UTF-8");
    digest.update(bytes, 0, bytes.length);
    byte[] key = digest.digest();

    log("SHA-256 key ", key);

    SecretKeySpec secretKeySpec = new SecretKeySpec(key, "AES");
    return secretKeySpec;
}

From source file:Main.java

/**
 * @param byteArray//from  ww w  .j a  v  a 2 s.  c o  m
 * @param algorithm 
 * @return byte[]
 * @throws NoSuchAlgorithmException 
 */
public static byte[] getEncryptedMessageDigest(final byte[] byteArray, final String algorithm)
        throws NoSuchAlgorithmException {
    return MessageDigest.getInstance(algorithm).digest(byteArray);
}

From source file:AddSHA1.java

public static String SHA1(String inStr) {
    MessageDigest md = null;/*from  www. ja v a  2s. c  o  m*/
    String outStr = null;
    try {
        md = MessageDigest.getInstance("SHA-1"); //SHA-1?MD5
        byte[] digest = md.digest(inStr.getBytes()); //byet[]?String
        outStr = bytetoString(digest);
    } catch (NoSuchAlgorithmException nsae) {
        nsae.printStackTrace();
    }
    return outStr;
}

From source file:Main.java

public static byte[] md5(String strObj) {
    MessageDigest md;/*from  ww w  . java2 s  .c  o m*/
    try {
        md = MessageDigest.getInstance("MD5");
        md.update(strObj.getBytes());
        byte[] pwd = md.digest();
        return pwd;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

/**
 * Print hash key/*ww w. j a v  a 2  s.  c  o  m*/
 */
public static void printHashKey(Context context) {
    try {
        PackageInfo info = context.getPackageManager().getPackageInfo(TAG, PackageManager.GET_SIGNATURES);
        for (Signature signature : info.signatures) {
            MessageDigest md = MessageDigest.getInstance("SHA");
            md.update(signature.toByteArray());
            String keyHash = Base64.encodeToString(md.digest(), Base64.DEFAULT);
            Log.d(TAG, "keyHash: " + keyHash);
        }
    } catch (NameNotFoundException e) {

    } catch (NoSuchAlgorithmException e) {

    }
}

From source file:Main.java

public static String generate(String s) {
    try {/*w  w w  . j a va 2 s  .  c  o m*/
        MessageDigest messageDigest = MessageDigest.getInstance("MD5");
        byte[] digest = messageDigest.digest(s.getBytes());
        StringBuilder md5 = new StringBuilder();
        for (byte value : digest) {
            md5.append(Integer.toHexString((value & 0xFF) | 0x100).substring(1, 3));
        }
        return md5.toString();
    } catch (NoSuchAlgorithmException e) {
        return null;
    }
}

From source file:com.basp.trabajo_al_minuto.model.business.BusinessSecurity.java

/**
 * Se encarga de encriptar la contrasea ingresada por el usuario *
 *///from  ww w .  ja  v  a2  s  .co  m
public static String encrypt(String value) throws BusinessException {
    String secretKey = "e-business";
    String base64EncryptedString = "";
    try {
        MessageDigest md = MessageDigest.getInstance("MD5");
        byte[] digestOfPassword = md.digest(secretKey.getBytes("utf-8"));
        byte[] keyBytes = Arrays.copyOf(digestOfPassword, 24);
        SecretKey key = new SecretKeySpec(keyBytes, "DESede");
        Cipher cipher = Cipher.getInstance("DESede");
        cipher.init(Cipher.ENCRYPT_MODE, key);
        byte[] plainTextBytes = value.getBytes("utf-8");
        byte[] buf = cipher.doFinal(plainTextBytes);
        byte[] base64Bytes = Base64.encodeBase64(buf);
        base64EncryptedString = new String(base64Bytes);
    } catch (Exception ex) {
        throw new BusinessException(ex);
    }
    return base64EncryptedString;
}

From source file:MainClass.java

static void performInputTest() throws Exception {
    MessageDigest md = MessageDigest.getInstance("SHA");
    FileInputStream fin = new FileInputStream("sha-results.txt");
    DigestInputStream in = new DigestInputStream(fin, md);
    byte[] b = new byte["testCase".getBytes().length];
    in.read(b, 0, "testCase".getBytes().length);
    md = in.getMessageDigest();//w w w  . j a v  a2  s  . co m
    String s = new String(md.digest());
    System.out.println("Calculated result:  " + s);
}

From source file:Main.java

public static String SHA1(String input) throws NoSuchAlgorithmException {
    MessageDigest mDigest = MessageDigest.getInstance("SHA1");
    byte[] result = mDigest.digest(input.getBytes());
    StringBuffer sb = new StringBuffer();
    for (int i = 0; i < result.length; i++) {
        sb.append(Integer.toString((result[i] & 0xff) + 0x100, 16).substring(1));
    }//from w  ww.  j  ava 2 s .c om

    return sb.toString();
}

From source file:Main.java

/**
 * Converts a String into a specified hash.
 * /*from   w ww.j a  v a2  s  . co m*/
 * @param text
 *            Text to convert.
 * @return hash.
 * @throws NoSuchAlgorithmException
 * @throws UnsupportedEncodingException
 */
public static String hashAlgorithm(String hash, String text)
        throws NoSuchAlgorithmException, UnsupportedEncodingException {
    //TapjoyLog.i(TAPJOY_UTIL, "" + hash + ": " + text);

    MessageDigest md;
    byte[] sha1hash = new byte[40];

    // MD5, SHA-1, etc
    md = MessageDigest.getInstance(hash);
    md.update(text.getBytes("iso-8859-1"), 0, text.length());
    sha1hash = md.digest();

    return convertToHex(sha1hash);
}