python方法实现java的hash算法

# -*- coding: utf-8 -*-
'''
Created on 2019-08-22 14:06
---------
@summary: python方法实现java的hash算法
---------
@author: yongxinYang
'''
class GetHashCode:

    def convert_n_bytes(self, n, b):
        bits = b * 8
        return (n + 2 ** (bits - 1)) % 2 ** bits - 2 ** (bits - 1)

    def convert_4_bytes(self, n):
        return self.convert_n_bytes(n, 4)

    @classmethod
    def getHashCode(cls, s):
        '''
        实现java的hash算法
        :param s:
        :return:
        '''
        h = 0
        n = len(s)
        for i, c in enumerate(s):
            h = h + ord(c) * 31 ** (n - 1 - i)
        return cls().convert_4_bytes(h)

    @classmethod
    def get_base_key(self,str):
        str2 = ''
        c = '65535'
        hash_num =self.getHashCode(str)
        if hash_num == -459336179:
            if str == "ACCOUNT":
                c = 0
                return "553a1fd4c7a4d23b2c05bb7b1438f978"

        elif hash_num == 75556:
            if str == "LOG":
                c = 1
                return "3666f801f6d69a75022ca5fc684fb312"
        elif hash_num == 76641:
            if str == "MSG":
                c = 2
                return "21c196b35b19963b6b934685f140b95b"

        elif hash_num == 2337004:
            if str == "LIVE":
                c = 3
                return "abb81dd045c9c995b988108e0024ed0c"
        elif hash_num == 2544374:
            if str == "SHOP":
                c = 4
                return "62dc47996c1dd6b23cecc9e29788b62b"
        elif hash_num == 67081473:
            if str == "FORUM":
                c = 5
                return "c5905376760d7a2cca10d5f684348f5c"
        elif hash_num == 25175104:
            if str == "STAT_CUBE":
                c = 6
                return "da2bdee10cb433a32d8aeaef114b6b6e"
        elif hash_num == 2288:
            if str == "GW":
                c = 7
                return "557ee139b69ca29910eb0c3d3057261f"
        else:
            return str2

if __name__ == "__main__":
    hashCode = GetHashCode.getHashCode("GRSW")
    print(hashCode)

    hashCode2 = GetHashCode.get_base_key("FORUM")
    print(hashCode2)

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值