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

public static void main(String[] args) throws Exception {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    DataOutputStream dos = new DataOutputStream(baos);

    MessageDigest md = MessageDigest.getInstance("MD5");
    SomeObject testObject = new SomeObject();

    dos.writeInt(testObject.count);//from   w w  w.  j a  v  a  2  s.c  om
    dos.writeLong(testObject.product);
    dos.writeDouble(testObject.stdDev);
    dos.writeUTF(testObject.name);
    dos.writeChar(testObject.delimiter);
    dos.flush();

    byte[] hashBytes = md.digest(baos.toByteArray());
    BigInteger testObjectHash = new BigInteger(hashBytes);

    System.out.println("Hash " + testObjectHash);

    dos.close();

}

From source file:md5.demo.MD5Demo.java

public static void main(String[] args) {
    String input = "123456";
    try {/*w  ww.ja v  a 2 s.  co  m*/
        MessageDigest messageDigest = MessageDigest.getInstance("MD5");
        byte[] output = messageDigest.digest(input.getBytes());
        String outString = Hex.encodeHexString(output);
        System.out.println(outString);

    } catch (NoSuchAlgorithmException ex) {
        ex.printStackTrace();
    }

}

From source file:MainClass.java

public static void main(String[] args) throws Exception {
    Security.addProvider(new BouncyCastleProvider());
    byte[] input = "www.java2s.com".getBytes();
    System.out.println("input     : " + new String(input));
    MessageDigest hash = MessageDigest.getInstance("SHA1");

    ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(input);
    DigestInputStream digestInputStream = new DigestInputStream(byteArrayInputStream, hash);
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

    int ch;//from  w  ww.  java  2s. c o m
    while ((ch = digestInputStream.read()) >= 0) {
        byteArrayOutputStream.write(ch);
    }

    byte[] newInput = byteArrayOutputStream.toByteArray();
    System.out.println("in digest : " + new String(digestInputStream.getMessageDigest().digest()));

    byteArrayOutputStream = new ByteArrayOutputStream();
    DigestOutputStream digestOutputStream = new DigestOutputStream(byteArrayOutputStream, hash);
    digestOutputStream.write(newInput);
    digestOutputStream.close();

    System.out.println("out digest: " + new String(digestOutputStream.getMessageDigest().digest()));
}

From source file:core.Hash.java

/**
 * @param args the command line arguments
 *///  w  w w.ja  v  a  2  s .  c o  m
public static void main(String[] args) {

    MessageDigest md = null;
    String password = "password D:";
    try {

        //SHA-512
        md = MessageDigest.getInstance("SHA-512");
        md.update(password.getBytes());
        byte[] mb = md.digest();
        System.out.println(Hex.encodeHex(mb));
        //SHA-1
        md = MessageDigest.getInstance("SHA-1");
        md.update(password.getBytes());
        mb = md.digest();
        System.out.println(Hex.encodeHex(mb));
        //MD5
        md = MessageDigest.getInstance("MD5");
        md.update(password.getBytes());
        mb = md.digest();
        System.out.println(Hex.encodeHex(mb));

    } catch (NoSuchAlgorithmException e) {
        //Error
    }
}

From source file:algoritmorsa_md5.MD5.java

/**
 * @param args the command line arguments
 *//*from  w w w .  jav  a  2  s.co  m*/
public static void main(String[] args) throws NoSuchAlgorithmException {
    // TODO code application logic here

    MessageDigest md = MessageDigest.getInstance(MessageDigestAlgorithms.MD5);
    md.update("yesica".getBytes());
    byte[] digest = md.digest();

    for (byte b : digest) {
        System.out.println(Integer.toHexString(0xFF & b));
    }
    System.out.println();
    byte[] encoded = Base64.encodeBase64(digest);
    System.out.println(new String(encoded));
}

From source file:MainClass.java

public static void main(String args[]) throws Exception {
    FileInputStream fis = new FileInputStream("test");
    ObjectInputStream ois = new ObjectInputStream(fis);
    Object o = ois.readObject();/*from  w  ww.j  a v  a  2s  .  c  om*/
    if (!(o instanceof String)) {
        System.out.println("Unexpected data in file");
        System.exit(-1);
    }
    String data = (String) o;
    System.out.println("Got message " + data);
    o = ois.readObject();
    if (!(o instanceof byte[])) {
        System.out.println("Unexpected data in file");
        System.exit(-1);
    }
    byte origDigest[] = (byte[]) o;
    MessageDigest md = MessageDigest.getInstance("SHA");
    md.update(data.getBytes());
    if (MessageDigest.isEqual(md.digest(), origDigest))
        System.out.println("Message is valid");
    else
        System.out.println("Message was corrupted");
}

From source file:MainClass.java

public static void main(String args[]) throws Exception {
    FileInputStream fis = new FileInputStream("test");
    ObjectInputStream ois = new ObjectInputStream(fis);
    Object o = ois.readObject();//  w w w  .  j  a va 2s  .  com
    if (!(o instanceof String)) {
        System.out.println("Unexpected data in file");
        System.exit(-1);
    }
    String data = (String) o;
    System.out.println("Got message " + data);
    o = ois.readObject();
    if (!(o instanceof byte[])) {
        System.out.println("Unexpected data in file");
        System.exit(-1);
    }
    byte origDigest[] = (byte[]) o;
    byte pass[] = "aaa".getBytes();
    byte buf[] = data.getBytes();
    MessageDigest md = MessageDigest.getInstance("SHA");
    md.update(pass);
    md.update(buf);
    byte digest1[] = md.digest();
    md.update(pass);
    md.update(digest1);
    System.out.println(MessageDigest.isEqual(md.digest(), origDigest));
}

From source file:MainClass.java

public static void main(String args[]) {
    MessageDigest algorithm = null;
    Security.addProvider(new MyProvider());
    try {// ww  w.  ja v a 2 s  .c  o m
        algorithm = MessageDigest.getInstance("CRC32");
    } catch (NoSuchAlgorithmException e) {
        System.err.println("Invalid algorithm");
        System.exit(-1);
    }
    int argsLength = args.length;
    for (int i = 0; i < argsLength; i++) {
        String digest = computeDigest(algorithm, args[i]);
        System.out.println(args[i] + " : " + digest);

    }
}

From source file:com.daon.identityx.utils.GenerateAndroidFacet.java

public static void main(String[] args) {

    String androidKeystoreLocation = System.getProperty("ANDROID_KEYSTORE_LOCATION",
            DEFAULT_ANDROID_KEYSTORE_LOCATION);
    String androidKeystorePassword = System.getProperty("ANDROID_KEYSTORE_PASSWORD",
            DEFAULT_ANDROID_KEYSTORE_PASSWORD);
    String androidKeystoreCert = System.getProperty("ANDROID_KEYSTORE_CERT_NAME",
            DEFAULT_ANDROID_KEYSTORE_CERT_NAME);
    String hashingAlgorithm = System.getProperty("HASHING_ALGORITHM", DEFAULT_HASHING_ALGORITHM);

    try {/*from  w  w  w. j ava2s  . c  o  m*/
        KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
        File filePath = new File(androidKeystoreLocation);
        if (!filePath.exists()) {
            System.err.println(
                    "The filepath to the debug keystore could not be located at: " + androidKeystoreCert);
            System.exit(1);
        } else {
            System.out.println("Found the Android Studio keystore at: " + androidKeystoreLocation);
        }

        keyStore.load(new FileInputStream(filePath), androidKeystorePassword.toCharArray());
        System.out.println("Keystore loaded - password and location were OK");

        Certificate cert = keyStore.getCertificate(androidKeystoreCert);
        if (cert == null) {
            System.err.println(
                    "Could not location the certification in the store with the name: " + androidKeystoreCert);
            System.exit(1);
        } else {
            System.out.println("Certificate found in the store with name: " + androidKeystoreCert);
        }

        byte[] certBytes = cert.getEncoded();

        MessageDigest digest = MessageDigest.getInstance(hashingAlgorithm);
        System.out.println("Hashing algorithm: " + hashingAlgorithm + " found.");
        byte[] hashedCert = digest.digest(certBytes);
        String base64HashedCert = Base64.getEncoder().encodeToString(hashedCert);
        System.out.println("Base64 encoded SHA-1 hash of the certificate: " + base64HashedCert);
        String base64HashedCertRemoveTrailing = StringUtils.deleteAny(base64HashedCert, "=");
        System.out.println(
                "Add the following facet to the Facets file in order for the debug app to be trusted by the FIDO client");
        System.out.println("\"android:apk-key-hash:" + base64HashedCertRemoveTrailing + "\"");

    } catch (Throwable ex) {
        ex.printStackTrace();
    }

}

From source file:com.glaf.core.security.DigestUtil.java

public static void main(String[] args) throws Exception {
    MessageDigest md5 = MessageDigest.getInstance("MD5");
    md5.update("111111".getBytes());
}