Esempio n. 1
0
 def test_fnv1a_64(self):
     """
     Tests the 64 bit FNV-1a hash implementation.
     """
     for string, expected_hval in vector.fnv1a_64_vector.items():
         result = fnvhash.fnv1a_64(string)
         self.assertEqual(result, expected_hval)
Esempio n. 2
0
    def get_hash(self,
                 path: Union[str, bytes],
                 type: PATH_TYPES = None) -> int:
        """
        Calculates the 64 bit FNA1a hash value for a given path

        Parameters
        ----------
        path
            path to calculate the hash for
        type
            type of the path (i.e. whether this is a file or directory)

            if not given, it is attempted to infer from the path

        Returns
        -------
        Calculated 64bit FNV1a hash value
        """
        if isinstance(path, str):
            path = path.encode('utf-8')
        elif not isinstance(path, bytes):
            raise TypeError('path must be a string')

        if path.endswith(b'/'):
            if type is None:
                type = PATH_TYPES.DIR
            path = path.strip(b'/')
        # If type wasn't set before, assume this is a file
        if type == PATH_TYPES.FILE or type is None:
            path = path.lower()
        path += b'++'

        return fnv1a_64(path)
Esempio n. 3
0
    def _hash_tags(self, tags):
        hashes = []
        for tag in tags:
            assert isinstance(tag, basestring)
            hashes.append((fnv1a_64(tag)))

        return sum(hashes) % ((2**63) - 1)
Esempio n. 4
0
 def test_fnv1a_64(self):
     """
     Tests the 64 bit FNV-1a hash implementation.
     """
     for string, expected_hval in vector.fnv1a_64_vector.items():
         result = fnvhash.fnv1a_64(string)
         self.assertEqual(result, expected_hval)
Esempio n. 5
0
 def ahash(self):
     data = b''
     data += bytes(self.filename, "ascii")
     data += self.size.to_bytes(8, 'big')
     data += bytes(self.md5, "ascii")
     data += bytes(self.sha1, "ascii")
     data += bytes(self.sha256, "ascii")
     digest = fnv1a_64(data)
     return digest
Esempio n. 6
0
def save_image(url, download_dir):
    """ Download the image and save. """
    r = requests.get(url, stream=True)
    # print(convert_bytes_to_numpy(BytesIO(r.content)))
    data = read_convert_image_from_url(url)
    hashed = str(fnv1a_64(url.encode('utf-8')))
    filename = hashed + '.jpeg'
    key = os.path.join(download_dir, filename)
    with open(key, 'wb') as f:
        f.write(data.read())
def persist_image(file_persister, url, category_name):
    """ Download the image and save. """
    bucket_name = 'slavs-lambda-scraper'
    data = read_convert_image_from_url(url)
    hashed = str(fnv1a_64(url.encode('utf-8')))
    filename = hashed + '.jpeg'
    key = os.path.join(category_name, filename)
    file_persister.set_bucket(bucket_name)
    file_persister.put_object(data,
                              key,
                              ContentType='image/jpeg',
                              Metadata={'original_url': url})
Esempio n. 8
0
def hashme(s, hashType):
    if hashType == "md4":
        return "0x" + hashlib.new("md4", s).hexdigest()
    elif hashType == "md5":
        return "0x" + hashlib.new("md5", s).hexdigest()
    elif hashType == "sha1":
        return "0x" + hashlib.new("sha1", s).hexdigest()
    elif hashType == "sha224":
        return "0x" + hashlib.new("sha224", s).hexdigest()
    elif hashType == "sha256":
        return "0x" + hashlib.new("sha256", s).hexdigest()
    elif hashType == "sha384":
        return "0x" + hashlib.new("sha384", s).hexdigest()
    elif hashType == "sha512":
        return "0x" + hashlib.new("sha512", s).hexdigest()
    elif hashType == "ripemd160":
        return "0x" + hashlib.new("ripemd160", s).hexdigest()
    elif hashType == "whirlpool":
        return "0x" + hashlib.new("whirlpool", s).hexdigest()
    elif hashType == "crc8":
        return hex(crc8(s))
    elif hashType == "crc16":
        return hex(crc16(s))
    elif hashType == "crc32":
        return hex(crc32(s))
    elif hashType == "crc64":
        return hex(crc64(s))
    elif hashType == "djb2":
        return hex(djb2(s))
    elif hashType == "sdbm":
        return hex(sdbm(s))
    elif hashType == "loselose":
        return hex(loselose(s))
    elif hashType == "fnv1_32":
        return hex(fnvhash.fnv1_32(s))
    elif hashType == "fnv1a_32":
        return hex(fnvhash.fnv1a_32(s))
    elif hashType == "fnv1_64":
        return hex(fnvhash.fnv1_64(s))
    elif hashType == "fnv1a_64":
        return hex(fnvhash.fnv1a_64(s))
    elif hashType == "murmur3":  # this might also take a different seed
        return hex(mmh3.hash(s, signed=False))
Esempio n. 9
0
 def _hash_key(self, key) -> int:
     bytes_ = self.key_mem_type.to_bytes(key)
     return fnvhash.fnv1a_64(bytes_)
Esempio n. 10
0
    rawDataFull = 0
    fileSize = 0
    HASHFIELD_SIZE = 8
    with open(binFile, 'rb') as f:
        f.seek(0)
        fileSize = os.path.getsize(binFile)
        rawDataCropped = bytes(f.read(fileSize - HASHFIELD_SIZE))

        f.seek(0)
        rawDataFull = bytearray(f.read())

        f.close()

    # hash
    HASH_SEED = 0xcbf29ce484222325
    hash = fnv1a_64(rawDataCropped, HASH_SEED)
    print("  Calculated firmware hash: " + str(hex(hash)))

    # read out raw elf file and look for file offset of .firmwareCRC section for patching
    elfFile = binFile[:-3]
    elfFile += "elf"
    #rawElfFile = 0
    #firmwareCRCOffset = 0
    with open(elfFile, 'rb') as f:
        parsedElf = ELFFile(f)
        section = parsedElf.get_section_by_name(".firmwareHash")
        if section is None:
            raise Exception("Couldn't find firmwareHash section")
        firmwareCRCOffset = section.header.sh_offset
        f.seek(0)
        rawElfFile = bytearray(f.read())
Esempio n. 11
0
 def _get_event_id(self, event_sequence_number: int) -> bytes:
     # Apply the fnv1a hash on the Little-endian encoding of the
     # (sequence number, eu-id) tuple
     event_id_int = fnv1a_64(struct.pack(    # Little-endian encoding
                           "<Q",  event_sequence_number) + self.id)
     return struct.pack("<Q", event_id_int)
Esempio n. 12
0
def GetHash(svc):

    # encode proc/service using FNV-1a 64bit hash function
    fnv_val = fnv1a_64(svc.encode())

    return fnv_val ^ 6605813339339102567