1# Copyright 2019 Google LLC. 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); 4# you may not use this file except in compliance with the License. 5# You may obtain a copy of the License at 6# 7# https://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, 11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12# See the License for the specific language governing permissions and 13# limitations under the License. 14 15 16"""A list of supported hash functions.""" 17 18import hashlib 19 20 21class HashType: 22 """A wrapper around a hash function.""" 23 24 def __init__(self, bit_length: int, name: str): 25 self.bit_length = bit_length 26 self.name = name 27 28 def hash(self, data: bytes) -> int: 29 """Hashes a sequence of bytes to an integer.""" 30 hasher = hashlib.new(self.name) 31 hasher.update(data) 32 return int(hasher.hexdigest(), 16) 33 34 35HashType.SHA256 = HashType(256, 'sha256') 36HashType.SHA384 = HashType(384, 'sha384') 37HashType.SHA512 = HashType(512, 'sha512') 38