Hash function

General FreeBASIC programming questions.
ecxjoe
Posts: 83
Joined: Aug 08, 2009 6:01
Contact:

Hash function

Postby ecxjoe » Dec 04, 2014 7:30

Does freebasic have any built-in methods or libraries that can let me do this, for creating an md5 hash or something similar?
ecxjoe
Posts: 83
Joined: Aug 08, 2009 6:01
Contact:

Re: Hash function

Postby ecxjoe » Dec 04, 2014 7:39

sha1 ideally
TJF
Posts: 3581
Joined: Dec 06, 2009 22:27
Location: N47°, E15°
Contact:

Re: Hash function

Postby TJF » Dec 04, 2014 7:55

This package from MOD claims to support Md5, SHA1, SHA512 (untested)

MOD
Posts: 555
Joined: Jun 11, 2009 20:15

Re: Hash function

Postby MOD » Dec 04, 2014 12:03

You can also use mdTypes, it supports the same algorithms.
stephanbrunker
Posts: 62
Joined: Nov 02, 2013 14:57

Re: Hash function

Postby stephanbrunker » Dec 16, 2014 16:26

If you're looking for something with the source in FreeBasic, i've implemented BLAKE-256 (derivated from Salsa20, SHA-3 finalist and pretty fast) and Keccak 1600/1024 and 800/480 (SHA-3). I just have to check if the results are exactly the same as in the documentation because of Bit interleaving and Endian issues ... which are irrelevant for the security.
MichaelW
Posts: 3500
Joined: May 16, 2006 22:34
Location: USA

Re: Hash function

Postby MichaelW » Dec 27, 2014 18:28

Under Windows you can use the Cryptographic Service Providers.

Here is a quick example with no error checking, coded for the 32-bit compiler because, unlike the 64-bit compiler (or at least the package that I have), it includes the necessary header files:

Code: Select all

#include "windows.bi"
#include "win\wincrypt.bi"

#define ALG_SID_SHA_256 12
#define CALG_SHA_256 ALG_CLASS_HASH or ALG_TYPE_ANY or ALG_SID_SHA_256

dim as HCRYPTPROV hProv
dim as HCRYPTHASH hHash
dim as byte ptr pbData
dim as integer dwDataLen
dim as string szSample = "abc"
dim as string szExpected = _
    "BA7816BF8F01CFEA414140DE5DAE2223B00361A396177A9CB410FF61F20015AD"

CryptAcquireContext(@hProv,NULL,NULL,PROV_RSA_AES,CRYPT_VERIFYCONTEXT)

CryptCreateHash(hProv,CALG_SHA_256,0,0,@hHash)

CryptHashData(hHash,strptr(szSample),len(szSample),0)   

CryptGetHashParam(hHash,HP_HASHVAL,NULL,@dwDataLen,0)

print dwDataLen

pbData = allocate(dwDataLen)

CryptGetHashParam(hHash,HP_HASHVAL,pbData,@dwDataLen,0)

print szSample
print szExpected

for i as integer = 0 to dwDataLen-1
    print hex(pbData[i],2);
next
print

CryptDestroyHash(hHash)

CryptReleaseContext(hProv,0)

sleep

Code: Select all

 32
abc
BA7816BF8F01CFEA414140DE5DAE2223B00361A396177A9CB410FF61F20015AD
BA7816BF8F01CFEA414140DE5DAE2223B00361A396177A9CB410FF61F20015AD

A large amount of test data is available here.

Return to “General”

Who is online

Users browsing this forum: No registered users and 8 guests