List of usage examples for java.security MessageDigest digest
public byte[] digest()
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 a va 2 s . 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 ww w .jav 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();// w w w .j a v a2 s. c o 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; 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[]) 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 w w w .jav a2 s . c om*/ 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: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();/*ww w . j a v a 2s. co 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:algoritmorsa_md5.MD5.java
/** * @param args the command line arguments *//* ww w . ja v a 2 s . com*/ 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 { MessageDigest m = MessageDigest.getInstance("MD5"); FileInputStream fin = new FileInputStream(args[0]); DigestInputStream din = new DigestInputStream(fin, m); while (din.read() != -1) ;// w w w. ja v a 2s . co m 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: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 w w.j a v a2 s . c om*/ } byte[] result = sha.digest(); System.out.println(Arrays.toString(result)); System.out.println(new BigInteger(result)); }
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 ww w .j av a 2 s . co m oos.writeObject(data); oos.writeObject(md.digest()); }
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 .j a v a2s . c o m*/ 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())); }