Created
February 20, 2021 16:55
-
-
Save Katafalkas/689ae482f30d386e0f7c5b93419a82a1 to your computer and use it in GitHub Desktop.
File hashing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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