Created
March 2, 2022 16:25
-
-
Save Brodan/3fc4fc3de7012f954937cdf1841cb585 to your computer and use it in GitHub Desktop.
calculate the provenance hash of a directory full of files (e.g. images). requires hardhat
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
// run the script with `npx hardhat run <script>` | |
import path from 'path' | |
import fs from 'fs' | |
import { createHash } from 'crypto' | |
const CSV_FILENAME = "provenance_record.csv" | |
async function main() { | |
// CHANGE THE PATH HERE TO YOUR IMAGE COLLECTION PATH | |
const imageDirectory = path.join(__dirname, '/../../images/output/image') | |
const files = fs.readdirSync(imageDirectory) | |
const hashes: string[] = [] | |
let csvData = "Index,Filename,Hash" | |
// get every individual image hash | |
for (let i = 0; i < files.length; i++) { | |
const fp = path.join(imageDirectory, files[i]) | |
const fd = fs.readFileSync(fp) | |
const hash = createHash('sha256').update(fd).digest('hex') | |
hashes.push(hash) | |
csvData += `\r\n${i},${files[i]},${hash}` | |
} | |
// make one big string and hash it | |
const concatenatedHashString = hashes.join('') | |
const result = createHash('sha256').update(concatenatedHashString).digest('hex') | |
console.log(`Final hash: ${result}`) | |
// write to csv because why not | |
fs.writeFileSync(CSV_FILENAME, csvData) | |
console.log(`CSV written to ${CSV_FILENAME}`) | |
} | |
main() | |
.then(() => process.exit(0)) | |
.catch((error) => { | |
console.error(error) | |
process.exit(1) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment