Example usage for java.security MessageDigest update

List of usage examples for java.security MessageDigest update

Introduction

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

Prototype

public final void update(ByteBuffer input) 

Source Link

Document

Update the digest using the specified ByteBuffer.

Usage

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);
    byte[] k = md5.digest(key);
}

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  ww.  ja  v a 2s  .  c o  m
    System.out.println(result);
}

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  w  w . j  a  v  a  2  s.  co  m
    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();//from   w w w .  j  ava 2 s .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;
    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: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   w  w  w  .ja  v  a 2s . c om
    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:algoritmorsa_md5.MD5.java

/**
 * @param args the command line arguments
 *//*from w  ww  .j ava  2  s.c  o  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 {
    Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
    MessageDigest sha = MessageDigest.getInstance("SHA-1");
    byte[] data1 = { 65, 66, 67, 68, 69 };

    sha.update(data1);
    byte[] msgDigest = sha.digest();

    System.out.println("--- Message Digest ---");
    for (int i = 0; i < msgDigest.length; i++) {
        System.out.print(msgDigest[i] + " ");
    }/*ww w  . jav a 2 s. co m*/
}

From source file:MainClass.java

public static void main(String[] args) throws Exception {
    Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());

    KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA", "BC");
    keyGen.initialize(512, new SecureRandom());
    KeyPair keyPair = keyGen.generateKeyPair();

    Signature signature = Signature.getInstance("SHA256withRSA", "BC");
    signature.initSign(keyPair.getPrivate());

    byte[] message = "abc".getBytes();
    signature.update(message);/*ww  w. ja va 2  s.c om*/

    byte[] sigBytes = signature.sign();
    Cipher cipher = Cipher.getInstance("RSA/None/PKCS1Padding", "BC");
    cipher.init(Cipher.DECRYPT_MODE, keyPair.getPublic());

    byte[] decSig = cipher.doFinal(sigBytes);
    ASN1InputStream aIn = new ASN1InputStream(decSig);
    ASN1Sequence seq = (ASN1Sequence) aIn.readObject();

    System.out.println(ASN1Dump.dumpAsString(seq));

    MessageDigest hash = MessageDigest.getInstance("SHA-256", "BC");
    hash.update(message);

    ASN1OctetString sigHash = (ASN1OctetString) seq.getObjectAt(1);
    System.out.println(MessageDigest.isEqual(hash.digest(), sigHash.getOctets()));
}

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());
}

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);
    oos.writeObject(data);/*  w  w w.j a  v a 2 s  .  c o m*/
    oos.writeObject(md.digest());
}