tags:

views:

2720

answers:

7

In my application I need to hash a string before I save it to a text file. Does anyone know how to do that?

A: 

You can try with CriptoASP. Don't mind about the 'ASP' in the name, it's an ActiveX DLL you can instantiate from VB6. It provides methods for generate random numbers, hashes with MD2, MD4, MD5 and SHA and some kind of encryption/decryption.

You can download it from CriptoASP

I'm sorry, web's language is in spanish, but I think you can undertand the very easy use of the COM with the examples at bottom of Doc. for CriptoASP, just change Server.CreateObject with the new object sentence in VB6.

Tell me if you want a translate of the examples or documentation.

To save it to a text file you can use FSO (File System Object).

ARemesal
+1  A: 

For what purpose are you using the hash? This matters because some hash algorithms such as MD5 are suitable for some purposes, but not others.

This link shows a VB6 implementation of MD5.

RoadWarrior
A: 

I have had very good results with this one, but the implementation is c

sdbm

this algorithm was created for sdbm (a public-domain reimplementation of ndbm) database library. it was found to do well in scrambling bits, causing better distribution of the keys and fewer splits. it also happens to be a good general hashing function with good distribution. the actual function is hash(i) = hash(i - 1) * 65599 + str[i]; what is included below is the faster version used in gawk. [there is even a faster, duff-device version] the magic constant 65599 was picked out of thin air while experimenting with different constants, and turns out to be a prime. this is one of the algorithms used in berkeley db (see sleepycat) and elsewhere.

static unsigned long
sdbm(str)
unsigned char *str;
{
    unsigned long hash = 0;
    int c;

    while (c = *str++)
        hash = c + (hash << 6) + (hash << 16) - hash;

    return hash;
}

To port it to vb, the calculation goes something like this. Basically it walks through the characters of the string from left to right, calculating the hash as

newHash = the character (c) + (previousHashValue * 2^6) + 
                              (previousHashValue * 2^16) - 
                               previousHashValue**
previousHashValue = newHash
EvilTeach
+1  A: 

Here's a CRC32 hash function:

http://www.vbcode.com/asp/showsn.asp?theID=471

Joel Spolsky
+2  A: 

Example of using CAPICOM to get a hash

Add CAPICOM.DLL as project reference

uses DIM key As String DIM sValue As String

Dim sEncrypedValue as String

Dim oCAP As CAPICOM.EncryptedData Set oCAP = New CAPICOM.EncryptedData

With oCAP. .Algorithm.KeyLength = CAPICOM_ENCRYPTION_KEY_LENGTH_56_BITS .Algorithm.Name = CAPICOM_ENCRYPTION_ALGORITHM_RC4
.SetSecret key .Content = sValue end with

sEncrypedValue = objCAP.Encrypt(CAPICOM_ENCODE_BASE64)

To Decrypt: oCAP.SetSecret key oCAP.Content = sEncrypedValue sValue = oCAP.Decrypt(CAPICOM_ENCODE_BASE64)

garykindel
A: 

You could implement it in a C#.net dll using SHA128Managed

John
+1  A: 

(commenting requires 50 rep)

The link that Joel provided is good, but note that you'll need to change the seed value to the standard for it to produce the same CRC32 as everyone else:

Optional ByVal Seed As Long = &HEDB88320

Hope that saves someone else 30 minutes of work!