1 文本格式
using System;
namespace Legalsoft.Truffer
{
public class HashAll
{
public HashAll()
{
}
/// <summary>
/// Pseudo-DES hashing of the 64-bit word(lword, rword). Both 32-bit arguments
/// are returned hashed on all bits.
/// </summary>
/// <param name="lword"></param>
/// <param name="rword"></param>
public static void psdes(ref uint lword, ref uint rword)
{
const int NITER = 2;
uint[] c1 = { 0xbaa96887, 0x1e17d32c, 0x03bcdc3c, 0x0f33d1b2 };
uint[] c2 = { 0x4b0f3b58, 0xe874f0c3, 0x6955c5a6, 0x55a7ca46 };
uint itmph = 0;
uint itmpl = 0;
for (uint i = 0; i < NITER; i++)
{
uint iswap = rword;
uint ia = (uint)((iswap) ^ c1[i]);
itmpl = ia & 0xffff;
itmph = (uint)(ia >> 16);
uint ib = itmpl * itmpl + ~(itmph * itmph);
rword = (uint)(lword ^ (((ia = (uint)((ib >> 16) | ((ib & 0xffff) << 16))) ^ c2[i]) + itmpl * itmph));
lword = iswap;
}
}
/// <summary>
/// Replace the array arr by a same-sized hash, all of whose bits depend on all
/// of the bits in arr.Uses psdes for the mutual hash of two 32-bit words.
/// </summary>
/// <param name="arr"></param>
/// <exception cref="Exception"></exception>
public static void hashall(uint[] arr)
{
int m = arr.Length;
int n = m - 1;
n |= n >> 1;
n |= n >> 2;
n |= n >> 4;
n |= n >> 8;
n |= n >> 16;
n++;
int nb = n;
int nb2 = n >> 1;
if (n < 2)
{
throw new Exception("size must be > 1");
}
while (nb > 1)
{
for (int jb = 0; jb < n - nb + 1; jb += nb)
{
for (int j = 0; j < nb2; j++)
{
if (jb + j + nb2 < m)
{
psdes(ref arr[jb + j], ref arr[jb + j + nb2]);
//uint jbj = arr[jb + j];
//uint jbjnb2 = arr[jb + j + nb2];
//psdes(ref jbj, ref jbjnb2);
//arr[jb + j] = jbj;
//arr[jb + j + nb2] = jbjnb2;
}
}
}
nb = nb2;
nb2 >>= 1;
}
nb2 = n >> 1;
if (m != n)
{
for (int j = nb2; j < m; j++)
{
psdes(ref arr[j], ref arr[j - nb2]);
//uint jbj = arr[j];
//uint jbjnb2 = arr[j - nb2];
//psdes(ref jbj, ref jbjnb2);
//arr[j] = jbj;
//arr[j - nb2] = jbjnb2;
}
}
}
}
}
2 代码格式
using System;namespace Legalsoft.Truffer
{public class HashAll{public HashAll(){}/// <summary>/// Pseudo-DES hashing of the 64-bit word(lword, rword). Both 32-bit arguments/// are returned hashed on all bits./// </summary>/// <param name="lword"></param>/// <param name="rword"></param>public static void psdes(ref uint lword, ref uint rword){const int NITER = 2;uint[] c1 = { 0xbaa96887, 0x1e17d32c, 0x03bcdc3c, 0x0f33d1b2 };uint[] c2 = { 0x4b0f3b58, 0xe874f0c3, 0x6955c5a6, 0x55a7ca46 };uint itmph = 0;uint itmpl = 0;for (uint i = 0; i < NITER; i++){uint iswap = rword;uint ia = (uint)((iswap) ^ c1[i]);itmpl = ia & 0xffff;itmph = (uint)(ia >> 16);uint ib = itmpl * itmpl + ~(itmph * itmph);rword = (uint)(lword ^ (((ia = (uint)((ib >> 16) | ((ib & 0xffff) << 16))) ^ c2[i]) + itmpl * itmph));lword = iswap;}}/// <summary>/// Replace the array arr by a same-sized hash, all of whose bits depend on all/// of the bits in arr.Uses psdes for the mutual hash of two 32-bit words./// </summary>/// <param name="arr"></param>/// <exception cref="Exception"></exception>public static void hashall(uint[] arr){int m = arr.Length;int n = m - 1;n |= n >> 1;n |= n >> 2;n |= n >> 4;n |= n >> 8;n |= n >> 16;n++;int nb = n;int nb2 = n >> 1;if (n < 2){throw new Exception("size must be > 1");}while (nb > 1){for (int jb = 0; jb < n - nb + 1; jb += nb){for (int j = 0; j < nb2; j++){if (jb + j + nb2 < m){psdes(ref arr[jb + j], ref arr[jb + j + nb2]);//uint jbj = arr[jb + j];//uint jbjnb2 = arr[jb + j + nb2];//psdes(ref jbj, ref jbjnb2);//arr[jb + j] = jbj;//arr[jb + j + nb2] = jbjnb2;}}}nb = nb2;nb2 >>= 1;}nb2 = n >> 1;if (m != n){for (int j = nb2; j < m; j++){psdes(ref arr[j], ref arr[j - nb2]);//uint jbj = arr[j];//uint jbjnb2 = arr[j - nb2];//psdes(ref jbj, ref jbjnb2);//arr[j] = jbj;//arr[j - nb2] = jbjnb2;}}}}
}