====== SHA Checksum ======
For more information on the SHA algorithm and pseudocode, visit [[http://en.wikipedia.org/wiki/SHA|the Wikipedia article]]
===== Code snippets =====
==== Language: C# ====
The following is an example of an SHA checksum in C# (using the System.Security.Cryptography namespace).
public class SHA1Test
{
public static byte[[]] Compute(byte[[]] input)
{
// Initialize the engine
SHA1 engine = new SHA1CryptoServiceProvider();
// Compute the hash
byte[[]] hash = engine.ComputeHash(input);
// Return the result
return hash;
} } <\/code> [[http://msdn2.microsoft.com/en-us/library/system.security.cryptography.sha1cryptoserviceprovider.aspx|MSDN documentation for the SHA1CryptoServiceProvider class]]
==== Language: Java ==== The following is an example of an SHA checksum in Java.
import java.security.MessageDigest;
public static String SHAsum(byte[[]] convertme) { MessageDigest md = MessageDigest.getInstance("SHA-1"); //This could also be SHA1withDSA return String sum = new String(md.digest(convertme)); } <\/code> [[http://java.sun.com/j2se/1.5.0/docs/api/java/security/MessageDigest.html|MessageDigest API from Sun]]
[[http://java.sun.com/j2se/1.4.2/docs/guide/security/CryptoSpec.html|Java Cryptography Architecture]]
==== Language: Perl ==== The following is an example of an SHA checksum in Perl.
- Functional style Perl 5.6.x or earlier
use Digest::file qw(digest_file_base64);
my $full_filename='c:/yourpath/yourfile.ext'; # on Linux # my $full_filename='c:\yourpath\yourfile.ext'; # on Windows my $digest=//; $digest = digest_file_base64( $file, 'SHA1' ) unless -d $file; # skip directories! END <\/code>
- Functional style Perl 5.8.x or later
use Digest::SHA1 qw(sha1 sha1_hex sha1_base64);
$digest = sha1($data); $digest = sha1_hex($data); $digest = sha1_base64($data); $digest = sha1_transform($data); <\/code>
==== Language: PHP ====
The following is an example of an SHA checksum in PHP.
$digest = sha1($data);
<\/code>
$digest = hash('sha1',$data);
<\/code>
Note that hash() is available in PHP 5 and above.
==== Language: Python ==== The following is an example of an SHA checksum in Python.
import hashlib
converted = hashlib.sha1("My text").hexdigest() <\/code>
[[http://docs.python.org/lib/module-hashlib.html|Python hashlib reference]]
==== Language: Tcl ==== The following is an example of a SHA1 checksum using Tcl.
package require sha1
set digest [[sha1::sha1|-hex "My text"]]
<\/code>
[[http://tcllib.sourceforge.net/doc/sha1.html|Tcllib sha1 manual page]]
===== See also ===== [[MD5_Checksum|MD5 checksum]]