哈希算法
用来产生一些数据片段(例如消息或会话项)的 哈希值
的算法。使用好的 哈希
算法,在输入数据中所做的更改就可以更改结果哈希值中的所有位;因此,哈希对于检测 数据对象
(例如消息)中的修改很有用。此外,好的哈希算法使得构造两个相互独立且具有相同哈希的输入不能通过计算方法实现。典型的哈希算法包括 MD2、MD4、MD5 和 SHA-1。哈希算法也称为“ 哈希函数
”。
另请参阅: 基于哈希的消息验证模式 (HMAC), MD2, MD4, MD5, 消息摘要
, 安全哈希算法 (SHA-1)
MD5一种符合工业标准的单向 128 位哈希方案,由 RSA Data Security, Inc. 开发。 各种“ 点对点协议
(PPP)”供应商都将它用于加密的 身份验证
。哈希方案是一种以结果唯一并且不能返回到其原始格式的方式来转换数据(如密码)的方法。质询握手 身份验证协议
(CHAP) 使用质询响应并在响应时使用单向 MD5 哈希法。按照此方式,您无须通过网络发送密码就可以向服务器证明您知道密码。
质询握手身份验证协议 (CHAP)“点对点协议 (PPP)”连接的一种质询响应验证协议,在 RFC 1994 中有所描述。 该协议使用业界标准 MD5 哈希算法来哈希质询串(由身份验证服务器所发布)和响应中的用户密码的组合。
点对点协议 (PPP)
用 点对点
链接来传送多协议数据报的行业标准协议套件。RFC 1661 中有关于 PPP 的文档。
另请参阅: 压缩控制协议 (CCP), 远程访问
, 征求意见文档 (RFC), 传输控制协议
/Internet 协议 (TCP/IP), 自主隧道
几个比较著名的哈希算法
class GeneralHashFunctionLibrary
{
public long RSHash(String str)
{
int b = 378551;
int a = 63689;
long hash = 0;
for(int i = 0; i < str.length(); i++)
{
hash = hash * a + str.charAt(i);
a = a * b;
}
return hash;
}
public long JSHash(String str)
{
long hash = 1315423911;
for(int i = 0; i < str.length(); i++)
{
hash ^= ((hash << 5) + str.charAt(i) + (hash >> 2));
}
return hash;
}
public long PJWHash(String str)
{
long BitsInUnsignedInt = (long)(4 * 8);
long ThreeQuarters = (long)((BitsInUnsignedInt * 3) / 4);
long OneEighth = (long)(BitsInUnsignedInt / 8);
long HighBits = (long)(0xFFFFFFFF) << (BitsInUnsignedInt - OneEighth);
long hash = 0;
long test = 0;
for(int i = 0; i < str.length(); i++)
{
hash = (hash << OneEighth) + str.charAt(i);
if((test = hash & HighBits) != 0)
{
hash = (( hash ^ (test >> ThreeQuarters)) & (~HighBits));
}
}
return hash;
}
public long ELFHash(String str)
{
long hash = 0;
long x = 0;
for(int i = 0; i < str.length(); i++)
{
hash = (hash << 4) + str.charAt(i);
if((x = hash & 0xF0000000L) != 0)
{
hash ^= (x >> 24);
}
hash &= ~x;
}
return hash;
}
public long BKDRHash(String str)
{
long seed = 131; // 31 131 1313 13131 131313 etc..
long hash = 0;
for(int i = 0; i < str.length(); i++)
{
hash = (hash * seed) + str.charAt(i);
}
return hash;
}
public long SDBMHash(String str)
{
long hash = 0;
for(int i = 0; i < str.length(); i++)
{
hash = str.charAt(i) + (hash << 6) + (hash << 16) - hash;
}
return hash;
}
public long DJBHash(String str)
{
long hash = 5381;
for(int i = 0; i < str.length(); i++)
{
hash = ((hash << 5) + hash) + str.charAt(i);
}
return hash;
}
public long DEKHash(String str)
{
long hash = str.length();
for(int i = 0; i < str.length(); i++)
{
hash = ((hash << 5) ^ (hash >> 27)) ^ str.charAt(i);
}
return hash;
}
public long BPHash(String str)
{
long hash = 0;
for(int i = 0; i < str.length(); i++)
{
hash = hash << 7 ^ str.charAt(i);
}
return hash;
}
public long FNVHash(String str)
{
long fnv_prime = 0x811C9DC5;
long hash = 0;
for(int i = 0; i < str.length(); i++)
{
hash *= fnv_prime;
hash ^= str.charAt(i);
}
return hash;
}
public long APHash(String str)
{
long hash = 0xAAAAAAAA;
for(int i = 0; i < str.length(); i++)
{
if ((i & 1) == 0)
{
hash ^= ((hash << 7) ^ str.charAt(i) ^ (hash >> 3));
}
else
{
hash ^= (~((hash << 11) ^ str.charAt(i) ^ (hash >> 5)));
}
}
return hash;
}
}