Differences

This shows you the differences between two versions of the page.

Link to this comparison view

testmessagedigest [2014/10/26 01:52] (current)
Line 1: Line 1:
 +==== Test generation of random session keys (in java) ====
 +<code lang="​java">​
 +import java.security.MessageDigest;​
 +import java.security.NoSuchAlgorithmException;​
  
 +public class TestEncryption {
 +
 + String msg = null;
 +
 + public TestEncryption() {
 + }
 +
 + public TestEncryption(String m) {
 + msg = m;
 + }
 +
 + // return MD5(string)
 + public String encrypt(String m) {
 + byte[] byteArray = m.getBytes();​
 + StringBuffer bld = new StringBuffer();​
 +     try {
 +     MessageDigest md = MessageDigest.getInstance("​MD5"​);​
 +    
 +     byte[] result = md.digest(byteArray);​
 +     // System.out.println(result.length);​
 +     for (int i=0; i<​result.length;​ i++) {
 +     String s = Integer.toHexString((int) result[i] & 0x0ff);
 +     if (1 == s.length()) s = "​0"​ + s;
 +     // System.out.print(s);​
 +     bld.append(s);​
 +     }
 +     // System.out.println("​ ");
 +     } catch (NoSuchAlgorithmException e) {
 +     e.printStackTrace();​
 +     }
 + return bld.toString();​
 + }
 +
 + /**
 + * @param args
 + */
 + public static void main(String[] args) {
 + String msg = "A very long string. This is more than 16 bytes long";
 + TestEncryption te = new TestEncryption();​
 + for (int i=500; i<1000; i++) {
 + String s = Integer.toString(i);​
 + System.out.println(i+ " : " +te.encrypt(s));​
 + }
 + }
 +
 +}
 +</​code>​
 +
 +----
 +  * [[javainfo|Back to Java]]
testmessagedigest.txt ยท Last modified: 2014/10/26 01:52 (external edit)
CC Attribution-Share Alike 3.0 Unported
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0