Last active
June 15, 2022 18:46
-
-
Save dangdennis/03d46c16ba85c225ffd6a6c916608b55 to your computer and use it in GitHub Desktop.
sha1 hash some files with Node
This file contains 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
/** | |
* package.json | |
* { "type": "module" } | |
*/ | |
import fs from 'fs/promises' | |
import path from 'path' | |
import { promisify } from 'util' | |
import { exec as cbExec } from 'child_process' | |
const exec = promisify(cbExec); | |
async function main(dirPath) { | |
const files = await fs.readdir(dirPath, "utf-8") | |
const fileHashes = {} | |
for (const f of files) { | |
const filePath = path.join(dirPath, f) | |
const { stdout } = await exec(`openssl sha1 ${filePath}`) | |
const hash = stdout.split("=")[1].slice(0, -1) | |
fileHashes[f] = hash | |
} | |
console.log(fileHashes) | |
} | |
main("your directory path") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment