zark 发表于 2015-11-15 14:59:57

LISP能不能做HASH算法?

Private Sub Initialize(ByVal vKeyString As String)

      Dim intI As Integer, intJ As Integer

      Randomize(Rnd(-1))'得到初始值(种子值)

      '每次调用初始值均相同

      '根据初始值(种子值)得到随机数序列,每次调用Initialize时,初始值均相同。只要vKeyString相同,所产生的随机数序列一定相同

      For intI = 1 To Len(vKeyString)

            intJ = Rnd(-Rnd * AscW(Mid(vKeyString, intI, 1)))

            Randomize(intJ)

      Next intI

    End Sub

    Public Sub DoXor(ByRef msFileText As String)

      Dim intC As Integer

      Dim intB As Integer

      Dim lngI As Long

      For lngI = 1 To Len(msFileText)

            intC = AscW(Mid(msFileText, lngI, 1))

            intB = Int(Rnd() * 2 ^ 7)

            '选用< =127可正确处理汉字,ChrW(n):n 有一个范围

            Mid(msFileText, lngI, 1) = ChrW(intC Xor intB)

      Next lngI



    End Sub

    Public Function Hash(ByVal ET As String) As String

      Dim BitLenString As String, KeyString As String,

      FileText As String

      BitLenString = "12345678"

      KeyString = ET & BitLenString

      Call Initialize(KeyString)

      '根据KeyString产生随机数序列

      FileText = ET & BitLenString

      Call DoXor(FileText)

      '根据上述随机数序列对FileText加密

      KeyString = FileText

      Call Initialize(KeyString)

      '根据上述的加密结果产生新的随机数序列

      FileText = BitLenString

      Call DoXor(FileText)

      '根据上述随机数序列对FileText加密,8位字符

      Hash = FileText

      '8位字符送作HASH值

    End Function



    Private Sub Button8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button8.Click

      Dim str As String = "abcd" & <a class="baidu-highlight" href="https://www.baidu.com/s?wd=vb&tn=44039180_cpr&fenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1Y3mym3nhwbm1T4mWb1nj6z0AP8IA3qPjfsn1bkrjKxmLKz0ZNzUjdCIZwsrBtEXh9GuA7EQhF9pywdQhPEUiqkIyN1IA-EUBtvnH6zrjf4nW04njcsrHDznHc" target="_blank">vb</a>CrLf



      TextBox6.Text = str & Hash(str)

    End Sub

-----------------------------------------------------------------------------------------------------------------------------------------------------


unsigned int SDBMHash(char *str)
{
    unsigned int hash = 0;

    while (*str)
    {
      // equivalent to: hash = 65599*hash + (*str++);
      hash = (*str++) + (hash << 6) + (hash << 16) - hash;
    }

    return (hash & 0x7FFFFFFF);
}

// RS Hash
unsigned int RSHash(char *str)
{
    unsigned int b = 378551;
    unsigned int a = 63689;
    unsigned int hash = 0;

    while (*str)
    {
      hash = hash * a + (*str++);
      a *= b;
    }

    return (hash & 0x7FFFFFFF);
}

// JS Hash
unsigned int JSHash(char *str)
{
    unsigned int hash = 1315423911;

    while (*str)
    {
      hash ^= ((hash << 5) + (*str++) + (hash >> 2));
    }

    return (hash & 0x7FFFFFFF);
}

// P. J. Weinberger Hash
unsigned int PJWHash(char *str)
{
    unsigned int BitsInUnignedInt = (unsigned int)(sizeof(unsigned int) * 8);
    unsigned int ThreeQuarters    = (unsigned int)((BitsInUnignedInt * 3) / 4);
    unsigned int OneEighth = (unsigned int)(BitsInUnignedInt / 8);
    unsigned int HighBits = (unsigned int)(0xFFFFFFFF) << (BitsInUnignedInt
                                             - OneEighth);
    unsigned int hash    = 0;
    unsigned int test    = 0;

    while (*str)
    {
      hash = (hash << OneEighth) + (*str++);
      if ((test = hash & HighBits) != 0)
      {
            hash = ((hash ^ (test >> ThreeQuarters)) & (~HighBits));
      }
    }

    return (hash & 0x7FFFFFFF);
}

// ELF Hash
unsigned int ELFHash(char *str)
{
    unsigned int hash = 0;
    unsigned int x    = 0;

    while (*str)
    {
      hash = (hash << 4) + (*str++);
      if ((x = hash & 0xF0000000L) != 0)
      {
            hash ^= (x >> 24);
            hash &= ~x;
      }
    }

    return (hash & 0x7FFFFFFF);
}

// BKDR Hash
unsigned int BKDRHash(char *str)
{
    unsigned int seed = 131; // 31 131 1313 13131 131313 etc..
    unsigned int hash = 0;

    while (*str)
    {
      hash = hash * seed + (*str++);
    }

    return (hash & 0x7FFFFFFF);
}

// DJB Hash
unsigned int DJBHash(char *str)
{
    unsigned int hash = 5381;

    while (*str)
    {
      hash += (hash << 5) + (*str++);
    }

    return (hash & 0x7FFFFFFF);
}

// AP Hash
unsigned int APHash(char *str)
{
    unsigned int hash = 0;
    int i;

    for (i=0; *str; i++)
    {
      if ((i & 1) == 0)
      {
            hash ^= ((hash << 7) ^ (*str++) ^ (hash >> 3));
      }
      else
      {
            hash ^= (~((hash << 11) ^ (*str++) ^ (hash >> 5)));
      }
    }

    return (hash & 0x7FFFFFFF);
}

cabinsummer 发表于 2015-11-15 14:59:58

可以参考一下Mac Lee的MD5算法LISP版
页: [1]
查看完整版本: LISP能不能做HASH算法?