summaryrefslogtreecommitdiff
path: root/lib/spack/spack/util/hash.py
blob: d2a32da99aa1ed76bc5cecbeaff1aa037363d13b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# Copyright 2013-2023 Lawrence Livermore National Security, LLC and other
# Spack Project Developers. See the top-level COPYRIGHT file for details.
#
# SPDX-License-Identifier: (Apache-2.0 OR MIT)

import base64
import hashlib

import spack.util.crypto


def b32_hash(content):
    """Return the b32 encoded sha1 hash of the input string as a string."""
    sha = hashlib.sha1(content.encode("utf-8"))
    b32_hash = base64.b32encode(sha.digest()).lower()
    b32_hash = b32_hash.decode("utf-8")
    return b32_hash


def base32_prefix_bits(hash_string, bits):
    """Return the first <bits> bits of a base32 string as an integer."""
    if bits > len(hash_string) * 5:
        raise ValueError("Too many bits! Requested %d bit prefix of '%s'." % (bits, hash_string))

    hash_bytes = base64.b32decode(hash_string, casefold=True)
    return spack.util.crypto.prefix_bits(hash_bytes, bits)