Skip to content

Instantly share code, notes, and snippets.

@Katafalkas
Created February 20, 2021 16:55
Show Gist options
  • Select an option

  • Save Katafalkas/689ae482f30d386e0f7c5b93419a82a1 to your computer and use it in GitHub Desktop.

Select an option

Save Katafalkas/689ae482f30d386e0f7c5b93419a82a1 to your computer and use it in GitHub Desktop.
File hashing
import base64
import hashlib
def hash_string(s):
obj_sha3_256 = hashlib.sha3_256(s)
return obj_sha3_256.hexdigest()
def file_hash(file_path):
with open(file_path, 'rb') as image_file:
encoded_string = base64.b64encode(image_file.read())
return hash_string(encoded_string)
def main():
print(file_hash('sample-car-image.jpg')) # a8eded631cb9c9279cfe3905e63de23098d20fad95f54bf703623db3535f3508
if __name__ == "__main__":
main()
const fs = require('fs').promises;
const { SHA3 } = require('sha3');
const imageHash = async (imageFilePath) => {
const fileContent = await fs.readFile(imageFilePath);
const base64FileContent = fileContent.toString('base64');
const hash = SHA3(256);
hash.update(base64FileContent);
return hash.digest({ buffer: Buffer.alloc(32), format: 'hex' });
};
const ih = imageHash('sample-car-image.jpg');
console.log(ih); // a8eded631cb9c9279cfe3905e63de23098d20fad95f54bf703623db3535f3508
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment