常用hash算法(转)

1、RSHash算法

public static int RSHash(byte[] bytes) {
        int hash = 0;
        int magic = 63689;
        for (byte b : bytes) {
            hash = hash * magic + b;
            magic = magic * 378551;
        }
        return hash;
    }

2、JSHash算法

 public static int JSHash(byte[] bytes) {
        int hash = 1315423911;
        for (byte b : bytes) {
            hash ^= ((hash << 5) + b + (hash >> 2));
        }
        return hash;
    }

3、ELFHash算法

public static int ELFHash(byte[] bytes) {
    int hash = 0;
    int x;
    for (byte b : bytes) {
        hash = (hash << 4) + b;
        if ((x = hash & 0xF0000000) != 0) {
            hash ^= (x >> 24);
            hash &= ~x;
        }
    }
    return hash;
}

4、BKDRHash算法

  public static int BKDRHash(byte[] bytes) {
        int seed = 131;
        int hash = 0;
        for (byte b : bytes) {
            hash = (hash * seed) + b;
        }
        return hash;
    }

5、BKDRHash算法

  public static int c(byte[] bytes) {
        int hash = 0;
        int len = bytes.length;
        for (int i = 0; i < len; i++) {
            if ((i & 1) == 0) {
                hash ^= ((hash << 7) ^ bytes[i] ^ (hash >> 3));
            } else {
                hash ^= (~((hash << 11) ^ bytes[i] ^ (hash >> 5)));
            }
        }
        return hash;
    }

6、DJBHash算法

 public static int DJBHash(byte[] bytes) {
        int hash = 5381;
        for (byte b : bytes) {
            hash = ((hash << 5) + hash) + b;
        }
        return hash;
    }

7、SDBMHash算法

 public static int SDBMHash(byte[] bytes) {
        int hash = 0;
        for (byte b : bytes) {
            hash = b + (hash << 6) + (hash << 16) - hash;
        }
        return hash;
    }

8、PJWHash算法

 public static int PJWHash(byte[] bytes) {
        long bitsInUnsignedInt = (4 << 3);
        long threeQuarters = ((bitsInUnsignedInt * 3) >> 2);
        long oneEighth = (bitsInUnsignedInt >> 3);
        long highBits = (long) (0xFFFFFFFF) << (bitsInUnsignedInt - oneEighth);
        int hash = 0;
        long test;
        for (byte b : bytes) {
            hash = (hash << oneEighth) + b;
            if ((test = hash & highBits) != 0) {
                hash = (int) ((hash ^ (test >> threeQuarters)) & (~highBits));
            }
        }
        return hash;
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值