Java Digest digest(String source, byte[] salt)

Here you can find the source of digest(String source, byte[] salt)

Description

digest

License

Open Source License

Declaration

public static String digest(String source, byte[] salt) 

Method Source Code


//package com.java2s;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class Main {
    /**//from  w w w  .j a  v a  2s .  com
     * Default encoding
     */
    private static final String DEFAULT_ENCODING = "utf-8";
    /**
     * Used to build output as Hex
     */
    private static final char[] DIGITS = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd',
            'e', 'f' };

    public static String digest(String source, byte[] salt) {
        try {
            return new String(encode(digest(source.getBytes(DEFAULT_ENCODING), salt)));
        } catch (UnsupportedEncodingException ex) {
            throw new RuntimeException("PasswordCrypter's error has occured!", ex);
        } catch (RuntimeException ex) {
            throw ex;
        }
    }

    private static byte[] digest(byte[] source, byte[] salt) {
        try {
            MessageDigest md5Digest = MessageDigest.getInstance("md5");
            md5Digest.reset();
            md5Digest.update(salt);
            return md5Digest.digest(source);
        } catch (NoSuchAlgorithmException ex) {
            throw new RuntimeException("PasswordCrypter's error has occured!", ex);
        }
    }

    public static String encode(byte[] data) {
        int len = data.length;
        char[] out = new char[len << 1];
        // two characters form the hex value.
        for (int i = 0, j = 0; i < len; i++) {
            out[j++] = DIGITS[(0xF0 & data[i]) >>> 4];
            out[j++] = DIGITS[0x0F & data[i]];
        }
        return new String(out);
    }
}

Related

  1. digest(String planeText)
  2. digest(String provider, File file, int radix)
  3. digest(String raw, String algorithm)
  4. digest(String s, String algorithm)
  5. digest(String source)
  6. digest(String source, String algorythm)
  7. digest(String strSource)
  8. digest(String strSrc, String encName)
  9. digest(String text)