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

public static void main(String args[]) throws Exception {
    MessageDigest m = MessageDigest.getInstance("MD5");
    m.update("test".getBytes());
    byte s[] = m.digest();
    String result = "";
    for (int i = 0; i < s.length; i++) {
        result += Integer.toHexString((0x000000ff & s[i]) | 0xffffff00).substring(6);
    }//from  w w  w. j av  a2 s  . c  o  m
    System.out.println(result);
}

From source file:Main.java

public static void main(String[] a) throws Exception {
    byte[] buffer = new byte[10000];
    byte[] key = new byte[8];

    MessageDigest md5 = MessageDigest.getInstance("MD5");
    md5.update(buffer);/*from   w  ww.  j  av a 2  s. co m*/
    byte[] k = md5.digest(key);
}

From source file:MainClass.java

public static void main(String args[]) throws Exception {
    MessageDigest m = MessageDigest.getInstance("MD5");
    FileInputStream fin = new FileInputStream(args[0]);
    DigestInputStream din = new DigestInputStream(fin, m);
    while (din.read() != -1)
        ;// w  w  w  . j  a va  2  s .c om

    byte s[] = m.digest();
    for (int i = 0; i < s.length; i++) {
        System.out.print(Integer.toHexString((0x000000ff & s[i]) | 0xffffff00).substring(6));
    }
}

From source file:MainClass.java

public static void main(String args[]) throws Exception {
    Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
    MessageDigest sha = MessageDigest.getInstance("SHA-1");
    byte[] data1 = { 65, 66, 67, 68, 69 };

    sha.update(data1);/*from  ww  w.  ja  v  a 2  s  .  c  o m*/
    byte[] msgDigest = sha.digest();

    System.out.println("--- Message Digest ---");
    for (int i = 0; i < msgDigest.length; i++) {
        System.out.print(msgDigest[i] + " ");
    }
}

From source file:MainClass.java

public static void main(String args[]) throws Exception {
    FileOutputStream fos = new FileOutputStream("test");
    MessageDigest md = MessageDigest.getInstance("SHA");
    ObjectOutputStream oos = new ObjectOutputStream(fos);
    String data = "thee";
    byte buf[] = data.getBytes();
    md.update(buf);/*from w  w w  .  ja  va 2  s.  c  o  m*/
    oos.writeObject(data);
    oos.writeObject(md.digest());
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    String password = "secret";
    String algorithm = "SHA";

    byte[] plainText = password.getBytes();

    MessageDigest md = MessageDigest.getInstance(algorithm);

    md.reset();//from  ww  w  .j  a  v  a2s.c o  m
    md.update(plainText);
    byte[] encodedPassword = md.digest();

    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < encodedPassword.length; i++) {
        if ((encodedPassword[i] & 0xff) < 0x10) {
            sb.append("0");
        }
        sb.append(Long.toString(encodedPassword[i] & 0xff, 16));
    }
    System.out.println("Plain    : " + password);
    System.out.println("Encrypted: " + sb.toString());
}

From source file:Main.java

public static void main(String args[]) throws Exception {
    InputStream fis = new FileInputStream("a.exe");

    byte[] buffer = new byte[1024];
    MessageDigest complete = MessageDigest.getInstance("MD5");
    int numRead;//from w ww. j a  v a 2  s.co m
    do {
        numRead = fis.read(buffer);
        if (numRead > 0) {
            complete.update(buffer, 0, numRead);
        }
    } while (numRead != -1);
    fis.close();

    byte[] b = complete.digest();
    String result = "";
    for (int i = 0; i < b.length; i++) {
        result += Integer.toString((b[i] & 0xff) + 0x100, 16).substring(1);
    }
    System.out.println(result);

}

From source file:Main.java

public static void main(String args[]) throws Exception {
    FileInputStream fis = new FileInputStream("test");
    MessageDigest md = MessageDigest.getInstance("SHA");
    DigestInputStream dis = new DigestInputStream(fis, md);
    ObjectInputStream ois = new ObjectInputStream(dis);
    Object o = ois.readObject();//from  w ww .j  a va 2 s . 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);
    dis.on(false);
    o = ois.readObject();
    if (!(o instanceof byte[])) {
        System.out.println("Unexpected data in file");
        System.exit(-1);
    }
    byte origDigest[] = (byte[]) o;
    System.out.println(MessageDigest.isEqual(md.digest(), origDigest));
}

From source file:MainClass.java

public static void main(String[] args) throws Exception {
    if (args.length != 1) {
        System.out.println("Usage: Masher filename");
        return;//  w w w.java 2  s.  c o  m
    }
    MessageDigest md = MessageDigest.getInstance("MD5");
    FileInputStream in = new FileInputStream(args[0]);
    byte[] buffer = new byte[8192];
    int length;
    while ((length = in.read(buffer)) != -1)
        md.update(buffer, 0, length);
    byte[] raw = md.digest();
    BASE64Encoder encoder = new BASE64Encoder();
    String base64 = encoder.encode(raw);
    System.out.println(base64);
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    URL u = new URL("http://www.google.com");
    InputStream in = u.openStream();
    MessageDigest sha = MessageDigest.getInstance("SHA");
    byte[] data = new byte[1024];
    int bytesRead = -1;
    while ((bytesRead = in.read(data)) >= 0) {
        sha.update(data, 0, bytesRead);//from  w ww.j  a  v a  2  s .c o  m
    }
    byte[] result = sha.digest();
    System.out.println(Arrays.toString(result));
    System.out.println(new BigInteger(result));
}