Cryptography Tutorials - Tutorial Addendum - Bulletin Abstract - SHA1 Algorithm

games appcolt utilitiesappsios mac appcolt
 31 December 18:00   

    



    



    

(Continued from antecedent part...)

    



    

Here is a sample program ming/java/general-java-list/' onMouseOver="tip('infobox1')" onMouseOut="untip()"> Java program to appearance you how to use the MessageDigest chic to accomplish some tests

    

on SHA1 algorithms.

    

 

    

/**

    

* JceSha1Test.java

    

* Absorb (c) 2006 by Dr. Yang, http://www.yang.com/

    

*/

    

import java.security.*;

    

class JceSha1Test {

    

accessible changeless abandoned main(String[] a) {

    

try {

    

MessageDigest md = MessageDigest.getInstance("SHA1");

    

System.out.println("Message abstract item info: ");

    

System.out.println(" Algorithm = "+md.getAlgorithm());

    

System.out.println(" Provider = "+md.getProvider());

    

System.out.println(" toString = "+md.toString());

    

Cord ascribe = "";

    

md.update(input.getBytes());

    

byte[] achievement = md.digest();

    

System.out.println();

    

System.out.println("SHA1(""+input+"") =");

    

System.out.println(" "+bytesToHex(output));

    

ascribe = "abc";

    

md.update(input.getBytes());

    

achievement = md.digest();

    

System.out.println();

    

System.out.println("SHA1(""+input+"") =");

    

System.out.println(" "+bytesToHex(output));

    

ascribe = "abcdefghijklmnopqrstuvwxyz";

    

md.update(input.getBytes());

    

achievement = md.digest();

    

System.out.println();

    

System.out.println("SHA1(""+input+"") =");

    

System.out.println(" "+bytesToHex(output));

    



    

} bolt (Exception e) {

    

System.out.println("Exception: "+e);

    

}

    

}

    

accessible changeless Cord bytesToHex(byte[] b) {

    

burn hexDigit[] = { 0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 ,

    

8 , 9 , A , B , C , D , E , F };

    

StringBuffer buf = new StringBuffer();

    

for (int j=0; j<b.length; j++) {

    

buf.append(hexDigit[(b[j] >> 4) & 0x0f]);

    

buf.append(hexDigit[b[j] & 0x0f]);

    

}

    

acknowledgment buf.toString();

    

}

    

}

    



    



    

If you run this sample program with JDK 1.5, you should get the afterward output:

    

 

    

Message abstract item info:

    

Algorithm = SHA1

    

Provider = SUN adaptation 1.5

    

toString = SHA1 Bulletin Abstract from SUN, <initialized>

    

SHA1("") =

    

DA39A3EE5E6B4B0D3255BFEF95601890AFD80709

    

SHA1("abc") =

    

A9993E364706816ABA3E25717850C26C9CD0D89D

    

SHA1("abcdefghijklmnopqrstuvwxyz") =

    

32D10C7B8CF96570CA04CE37F2A19D84240D3A89

    



    



    



    

SHA1 Accomplishing in PHP

    



    

If you are absorbed in using SHA1 in PHP, you can use the congenital action sha1().

    

Here is a sample program assuming you how to use sha1() function:

    

 

    

<?php # PhpSha1Test.php

    

# Absorb (c) 2006 by Dr. Yang, http://www.yang.com/

    

#

    

$input = "";

    

$output = sha1($input);

    

print("
");

    

print("SHA1("".$input."") =
");

    

print(" $output
");

    

$input = "abc";

    

$output = sha1($input);

    

print("
");

    

print("SHA1("".$input."") =
");

    

print(" $output
");

    

$input = "abcdefghijklmnopqrstuvwxyz";

    

$output = sha1($input);

    

print("
");

    

print("SHA1("".$input."") =
");

    

print(" $output
");

    

?>

    



    

(Continued on next part...)

    



    



    

 


Tags: program, system, message, sample, notes, string, tutorial, tutorials, object, print

 input, system, println, output, print, digest, message, bytestohex, tostring, algorithm, abcdefghijklmnopqrstuvwxyz, hexdigit, sample, output , program, messagedigest, update, getbytes, string, , system out, message digest, output sha1, sha1 input, print sha1, print output , bytestohex output, input print, println sha1, update input, input getbytes, digest system, println system, println bytestohex, println bytestohex output, update input getbytes, output sha1 input, sha1 input print, print output input, bytestohex output input, input getbytes output, buf append hexdigit, digest object info, tutorial notes message, tutorials tutorial notes, notes message digest, message digest sha1, message digest object, digest sha1 algorithm, cryptography tutorials tutorial,

Share Cryptography Tutorials - Tutorial Addendum - Bulletin Abstract - SHA1 Algorithm:
Digg it!   Google Bookmarks   Del.icio.us   Yahoo! MyWeb   Furl  Binklist   Reddit!   Stumble Upon   Technorati   Windows Live   Bookmark

Text link code :
Hyper link code:

Also see ...

Permalink
Article In : Computers & Technology  -  crypto