Mega Code Archive

 
Categories / Delphi / Functions
 

How to use a hash function

Title: How to use a hash function? Question: A typical hash function at work and the aim of such functions will be shown Answer: So first whats the use of a hash function: A hash function takes a long string (or message or digest) of any length as input and produces a fixed length string or integer as output, sometimes termed a message digest or a digital fingerprint. {************************************************************} {* input hash sum *} {* a maxfox... --hash function() -- DCFS456CA63223AD1256 *} {* 2354AEAAD435D12AEDF9 *} {************************************************************} So whats the connection between digital signatures and hash functions? For both security and performance reasons, most digital signature algorithms specify that only the digest of the message be "signed", not the entire message. Hash functions can also be used in the generation of pseudorandom bits. All major digital signature signing techniques (including DSA and RSA) involve first hashing the data then signing the hash. Raw message data is not signed because of both performance and security reasons. There is a Public License from TurboPower called LockBox. The components are LockBox from Turbopower - available at http://sourceforge.net/projects/tplockbox/ LockBox is a cross-platform toolkit for data encryption. It contains routines & components for use with Borland Delphi, C++Builder, & Kylix. It provides support for Blowfish, RSA, MD5, SHA-1, DES, triple- DES, Rijndael, & digital signing of messages. The interface of LBCIPHER.PAS v2.07 expects 3 parameters: procedure HashSHA1( var Digest: TSHA1Digest; const Buf; BufSize: Longint ); var Context: TSHA1Context; begin InitSHA1( Context ); UpdateSHA1( Context, Buf, BufSize ); FinalizeSHA1( Context, Digest ); end; you can choose between two hash functions: type TMD5Digest = array [0..15] of Byte; {128 bits - MD5} TSHA1Digest = array [0..19] of Byte; {160 bits - SHA-1} SHA-1 is considered to be the successor to MD5, an earlier, widely-used hash function. The SHA algorithms were designed by the National Security Agency (NSA) and published as a US government standard. The following procedure shows a call to the LockBox library: --------------------------------------------------- //fKey : array of byte; procedure TRijndaelCipher.KeyByPassword(const thePassword: string); var sha1: TSHA1Digest; pSha1: ^TSHA1Digest; cntr: integer; begin if Length(thePassword) 0 then begin HashSHA1(sha1,thePassword[1],Length(thePassword)); pSha1:= @fKey[0]; for cntr:= 1 to Length(fKey) div sizeof(TSHA1Digest) do begin pSha1^:= sha1; Inc(pSha1); end; Move(sha1, pSha1^,Length(fKey) mod sizeof(TSHA1Digest)); end else raise TRijndaelCipherException.CreateWithErrorCode('No valid password.',cRCECInvalidPassword); end; so the call of the hash is simple in HashSHA1(sha1,thePassword[1],Length(thePassword)); On .net1.1 and VCL.net we can use a straight forward solution like this: -------------------------------------------------- uses system.Security.Cryptography, system.Text; procedure TForm1.button1Click(sender: TObject); var arrStr: array of Byte; oHash: SHA1CryptoServiceProvider; oStream: TMemoryStream; begin oHash:= SHA1CryptoServiceProvider.create; oStream:= TMemoryStream.create; arrStr:= BytesOf('maxfox in a box'); oStream.write(arrStr, lenght(arrStr)); showMessage(oHash.computeHash(oStream)); end; another use of a hash is to find data just by the name (string), so we don't need an index or a tree. Immagine each number (longint) defines a place so we just need the name to find his place again (name - value rule), here's an example of such a function: function MakeHash(const s: string): Longint; {small hash maker} var i: Integer; begin Result:= 0; for i:= 1 to Length(s) do Result:= ((Result shl 7) or (Result shr 25)) + Ord(s[i]); end; So the design of a hash shows a tough example of use: var md5: TMD5 data: array[0..nset] of byte; i: integer; begin with TMD5.create() do begin reset for i:= 1 to sizeOf(data) -1 do add(data[i]); label2.caption:= digestStr; //output Free end; From http://en.wikipedia.org/wiki/Cryptographic_hash_functions: A typical use of a cryptographic hash would be as follows: Alice poses to Bob a tough math problem and claims she has solved it. Bob would like to try it himself, but would yet like to be sure that Alice is not bluffing. Therefore, Alice writes down her solution, appends a random nonce, computes its hash and tells Bob the hash value (whilst keeping the solution secret). This way, when Bob comes up with the solution himself a few days later, Alice can verify his solution but still be able to prove that she had the solution earlier. Hope Dan Brown will understand this ;)