Ejemplo n.º 1
0
def generateChecksum(key, method="base62"):
    """
    Generates a unique SHA1 based hash/checksum encoded as Base62 or Hex depending on the given parameters.

    :param key:
    :type key: str
    :param method:
    :type method: str

    """

    # Alternative hashing method using SIP keys:
    #
    # https://github.com/majek/pysiphash (Python library)
    # https://github.com/jedisct1/siphash-js (Node/JS library - for Core)
    #
    # if SIPHASH_SUPPORTED:
    #     sipkey = ("JASY" * 4).encode("ascii")
    #     self.__checksum2 = siphash.SipHash_2_4(sipkey).update(self.__key.encode("ascii")).hexdigest()
    #     print("SIP Checksum: %s" % self.__checksum2.decode("ascii"))

    sha1 = hashlib.sha1(key.encode("ascii"))
    if method == "base62":
        return Base62.encodeArrayToString(sha1.digest())
    else:
        return sha1.hexdigest()
Ejemplo n.º 2
0
def generateChecksum(key, method="base62"):
    """
    Generates a unique SHA1 based hash/checksum encoded as Base62 or Hex depending on the given parameters.

    :param key:
    :type key: str
    :param method:
    :type method: str

    """

    # Alternative hashing method using SIP keys:
    #
    # https://github.com/majek/pysiphash (Python library)
    # https://github.com/jedisct1/siphash-js (Node/JS library - for Core)
    #
    # if SIPHASH_SUPPORTED:
    #     sipkey = ("JASY" * 4).encode("ascii")
    #     self.__checksum2 = siphash.SipHash_2_4(sipkey).update(self.__key.encode("ascii")).hexdigest()
    #     print("SIP Checksum: %s" % self.__checksum2.decode("ascii"))

    sha1 = hashlib.sha1(key.encode("ascii"))
    if method == "base62":
        return Base62.encodeArrayToString(sha1.digest())
    else:
        return sha1.hexdigest()
Ejemplo n.º 3
0
def sha1(fileOrPath, block_size=2 ** 20):
    """Returns a SHA 1 checksum (as hex digest) of the given file (handle)"""

    if isinstance(fileOrPath, str):
        fileOrPath = open(fileOrPath, "rb")

    sha1res = hashlib.sha1()
    while True:
        data = fileOrPath.read(block_size)
        if not data:
            break
        sha1res.update(data)

    return Base62.encodeArrayToString(sha1res.digest())
Ejemplo n.º 4
0
def sha1(fileOrPath, block_size=2**20):
    """Returns a SHA 1 checksum (as hex digest) of the given file (handle)"""

    if isinstance(fileOrPath, str):
        fileOrPath = open(fileOrPath, "rb")

    sha1res = hashlib.sha1()
    while True:
        data = fileOrPath.read(block_size)
        if not data:
            break
        sha1res.update(data)

    return Base62.encodeArrayToString(sha1res.digest())