Using system.io; using system.security.cryptography;
Namespace Common {/// /// copyright (c), 2004, kwklover (伟) /// File name: Hasher.cs /// Author: Weike Version: 1.0 Date: April 22, 2004 / // Description: Hash (irreversible) encryption universal class library function /// summary> public class Hasher {private bote [] _hashkey; // hash key storage variable private string _hashtext; // String to be encrypted Public Hasher () {// // Todo: Add Construction Function Logic here //}
/// /// hash key /// summary> public Byte [] hashkey {set {_hashkey = value;} get {return _hashkey;}}
/// /// Need to generate a stream /// summary> public string hashtext {set {_hashtext = value;} get {return _hashtext;}}
/// /// Use the HMACSHA1 class to generate a hash sequence with a length of 20 bytes. The corresponding key is required to accept any size key. /// summary> /// returns> public string hmacsha1hasher () {byte [] hmackey = hashkey; byte [] hmacdata = system.text.encoding.utf8.getbytes (Hashtext);
Hmacsha1 hmac = new hmacsha1 (hmackey);
Cryptostream CS = New CryptostReam (stream.null, hmac, cryptostreammode.write); cs.write (hmacdata, 0, hmacdata.length); cs.close ();
Byte [] result = hmac.hash;
Return Convert.TOBASE64String (Result); // Return to 28 bytes of strings}
/// /// uses the MACTRIPLEDES class to generate a hash sequence with a length of 8 bytes. The corresponding key is required, the key length can be 8, 16 or 24 bytes of keys. /// summary> /// returns> public string mactribledeshasher () {byte [] mackey = hashkey; byte [] MacData = system.text.encoding.utf8.getbytes (Hashtext);
Mactripledes Mac = New Mactripledes (Mackey);
Byte [] result = mac.computehash (MacData);
Return Convert.TOBASE64STRING (RESULT); // Return to 12 byte strings}
/// /// uses the MD5CryptoServiceProvider class to generate a hash value. No key is required. /// summary> /// returns> public string md5hasher () {byte [] md5data = system.text.encoding.utf8.getbytes (Hashtext); MD5 MD5 = New MD5CryptoserviceProvider ();
Byte [] result = md5.computehash (md5data);
Return Convert.TOBASE64String (Result); // Return to 25 bytes string}
/// /// The length of 160-bit hashing value is generated using the Sha1managed class. No key is required. /// summary> /// returns> public string sha1managedhasher () {byte [] sha1data = system.text.encoding.utf8.getbytes (HashText);
Sha1Managed Sha1 = New Sha1Managed ();
Byte [] result = sha1.computehash (sha1data);
Return Convert.TOBASE64String (Result); // Returns a string of 28 bytes}
/// /// The length of 256-bit hash value is generated using the SHA256MANAGED class. No key is required. /// summary> /// returns> public string sha256managedhasher () {byte [] sha256data = system.text.Encoding.utf8.getbytes (Hashtext);
SHA256MANAGED SHA256 = New Sha256Managed ();
Byte [] result = sha256.computehash (sha256data);
Return Convert.TOBASE64String (Result); // Return the length of 44 bytes}
/// /// Use the SHA384MANAGED class to generate a length of 384-bit hash value. No key is required. /// summary> /// returns> public string sha384managedhasher () {byte [] sha384data = system.text.Encoding.utf8.getbytes (Hashtext);
SHA384MANAGED SHA384 = New SHA384Managed ();
Byte [] result = sha384.computehash (SHA384DATA);
Return Convert.TOBASE64STRING (Result); // Returns a string of 64 bytes}
/// /// Use the SHA512MANAGED class to generate a length of 512-bit hash value. No key is required. /// summary> /// returns> public string SHA512ManagedHasher () {byte [] SHA512Data = System.Text.Encoding.UTF8.GetBytes (HashText); SHA512Managed Sha512 = new SHA512Managed ();
BYTE [] Result = sha512.computehash (sha512data);
Return Convert.TOBASE64String (Result); // Return the string of length 88 bytes}}}}}}