python---哈希算法实现

# coding = utf-8


class Array:
    def __init__(self, size=32, init=None):
        self._size = size
        self._items = [init] * size

    def __getitem__(self, index):
        return self._items[index]

    def __setitem__(self, index, value):
        self._items[index] = value

    def __len__(self):
        return self._size

    def clear(self, value=None):
        for i in range(self._items):
            self._items[i] = value

    def __iter__(self):
        for item in self._items:
            yield item


class Slot:
    """
    定义一个哈希表数组的槽
    注意:一个槽有三种状态
    1. 从未被使用过,HashMap.UNUSED。 此槽没有被使用和冲突过,查找时只要找到UNUSED 就不用再继续探查了
    2. 使用过但是remove了, 此时是HashMap.EMPTY,该点后面的元素仍可能是有key
    3. 槽正在使用Slot 节点
    """
    def __init__(self, key, value):
        self.key = key
        self.value = value


class HashTable:
    # 表示slot 没有被使用过
    UNUSED = None
    # 使用过被删除
    EMPTY = Slot(None, None)

    def __init__(self):
        # 初始化,数组的每个元素都是UNUSED
        self._table = Array(8, init=HashTable.UNUSED)
        self.length = 0

    # 内置装饰器,把方法变成属性
    @property
    def __load_factor(self):
        return self.length / float(len(self._table))

    def __len__(self):
        return self.length

    def _hash(self, key):
        # abs函数返回绝对值 hash 是内置函数 _hash 直接使用内置的哈希函数,对数组的长度取模
        hash_str = abs(hash(key)) % len(self._table)
        # print(hash_str)
        return hash_str

    def _find_key(self, key):
        # 先用 _hash方法计算出槽的位置
        index = self._hash(key)
        # 先保存数组长度
        _len = len(self._table)

        # 如果这个槽不是没有被使用过
        while self._table[index] is not HashTable.UNUSED:
            # 如果这个槽是,曾经有过值,不过被删除了
            if self._table[index] is HashTable.EMPTY:
                # cpython 使用的一种解决哈希冲突的方式
                index = (index*5+1) % _len
                continue
            # 正在使用, 如果key值相同
            elif self._table[index].key == key:
                return index
            # 这里就只剩最后一种可能, 正在使用,但是key没有找到
            else:
                index = (index*5+1) % _len
        return None

    # 判断一个槽是否可以插入
    def _slot_can_insert(self, index):
        return self._table[index] is HashTable.EMPTY or self._table[index] is HashTable.UNUSED

    # 寻找一个空槽,用来插入
    def _find_slot_for_insert(self, key):
        index = self._hash(key)
        _len = len(self._table)
        while not self._slot_can_insert(index):
            index = (index*5+1) % _len
        # print(index)
        return index

    # 实现一个in操作符
    def __contains__(self, key):
        index = self._find_key(key)
        return index is not None

    def add(self, key, value):
        # 上面实现的in操作符
        if key in self:
            index = self._find_key(key)
            self._table[index].value = value
            # 返回False 表示没有执行插入操作,执行的是更新操作
            return False
        else:
            # 这两步可能会调用_slot_can_insert 函数,
            # 不管是哪种情况,EMPTY 或 是 UNUSEZD,
            # 都将这个节点声明为Slot类
            index = self._find_slot_for_insert(key)
            self._table[index] = Slot(key, value)
            self.length += 1
            # 当空间占用大于0.8 的时候,进行rehash 重新分配空间。
            if self.__load_factor >= 0.8:
                self._rehash()
            return True

    def _rehash(self):
        old_table = self._table
        new_size = len(self._table) * 2
        self._table = Array(new_size, HashTable.UNUSED)
        self.length = 0

        for slot in old_table:
            # 判断这个slot 是有值的
            if slot is not HashTable.UNUSED and slot is not HashTable.EMPTY:
                # 找到一个可以插入的槽
                index = self._find_slot_for_insert(slot.key)
                self._table[index] = slot
                self.length += 1

    def get(self, key, default=None):
        index = self._find_key(key)
        if index is None:
            return default
        else:
            return self._table[index].value

    def remove(self, key):
        index = self._find_key(key)
        if index is None:
            raise KeyError
        value = self._table[index].value
        self.length -= 1
        self._table[index] = HashTable.EMPTY
        return value

    # 遍历操作,python 字典默认遍历的是key,这里实现的也是遍历key
    def __iter__(self):
        for slot in self._table:
            if slot not in (HashTable.EMPTY, HashTable.UNUSED):
                yield slot.key


if __name__ == '__main__':
    h = HashTable()
    h.add('a', 0)
    h.add('b', 1)
    h.add('c', 2)
    assert len(h) == 3
    assert h.get('a') == 0
    assert h.get('b') == 1
    assert h.get('c') == 2
    assert h.get('sky') is None

    h.remove('a')
    assert h.get('a') is None
    assert sorted(list(h)) == ['b', 'c']

    for n in range(50):
        h.add(n, n)

    for i in range(50):
        assert h.get(n) == n

 

转载于:https://www.cnblogs.com/aguncn/p/10723147.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
SHA-256哈希算法是一种加密算法,用于将任意长度的消息转换为一个长度为256位的哈希值。下面是一个Python实现,该实现不使用任何函数库直接实现SHA-256哈希算法。 ```python import math # 初始化常量 h = [ 0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19 ] k = [ 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5, 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da, 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967, 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070, 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3, 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2 ] # 定义一些辅助函数 def rotr(x, n): return (x >> n) | (x << (32 - n)) def shr(x, n): return x >> n def ch(x, y, z): return (x & y) ^ (~x & z) def maj(x, y, z): return (x & y) ^ (x & z) ^ (y & z) def sigma0(x): return rotr(x, 2) ^ rotr(x, 13) ^ rotr(x, 22) def sigma1(x): return rotr(x, 6) ^ rotr(x, 11) ^ rotr(x, 25) def gamma0(x): return rotr(x, 7) ^ rotr(x, 18) ^ shr(x, 3) def gamma1(x): return rotr(x, 17) ^ rotr(x, 19) ^ shr(x, 10) # 定义SHA-256哈希函数 def sha256(message): # 对消息进行预处理 message = bytearray(message) ml = len(message) * 8 message.append(0x80) while (len(message) * 8) % 512 != 448: message.append(0x00) message += ml.to_bytes(8, byteorder='big') # 初始化哈希值 hash_values = h.copy() # 对消息进行分组 for i in range(0, len(message), 64): # 将每个分组划分为16个字 words = [int.from_bytes(message[j:j+4], byteorder='big') for j in range(i, i+64, 4)] # 扩展16个字为64个字 for j in range(16, 64): s0 = gamma0(words[j-15]) s1 = gamma1(words[j-2]) words.append((words[j-16] + s0 + words[j-7] + s1) & 0xffffffff) # 初始化临时变量 a, b, c, d, e, f, g, h = hash_values # 进行64轮的压缩 for j in range(64): S1 = sigma1(e) ch_val = ch(e, f, g) temp1 = (h + S1 + ch_val + k[j] + words[j]) & 0xffffffff S0 = sigma0(a) maj_val = maj(a, b, c) temp2 = (S0 + maj_val) & 0xffffffff h = g g = f f = e e = (d + temp1) & 0xffffffff d = c c = b b = a a = (temp1 + temp2) & 0xffffffff # 更新哈希值 hash_values[0] = (hash_values[0] + a) & 0xffffffff hash_values[1] = (hash_values[1] + b) & 0xffffffff hash_values[2] = (hash_values[2] + c) & 0xffffffff hash_values[3] = (hash_values[3] + d) & 0xffffffff hash_values[4] = (hash_values[4] + e) & 0xffffffff hash_values[5] = (hash_values[5] + f) & 0xffffffff hash_values[6] = (hash_values[6] + g) & 0xffffffff hash_values[7] = (hash_values[7] + h) & 0xffffffff # 将哈希值转换为16进制字符串 return ''.join(format(hv, '08x') for hv in hash_values) ``` 使用示例: ```python message = b'Hello, world!' print(sha256(message)) # 输出:b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9 ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值