Skip to content

Instantly share code, notes, and snippets.

@ValentaTomas
Last active October 13, 2023 20:13
Show Gist options
  • Save ValentaTomas/56aa5fd6f8c1121a237fdd1968d95bba to your computer and use it in GitHub Desktop.
Save ValentaTomas/56aa5fd6f8c1121a237fdd1968d95bba to your computer and use it in GitHub Desktop.
Hash files
import * as crypto from 'crypto'
const delimiter = '\0'
export function getFilesHash(files: { name: string; content: string }[]) {
const shasum = crypto.createHash('sha1')
files.forEach(({ name, content }) => {
shasum.update(name)
// Add delimiter to hash to prevent collisions between files where the join of the name and content is the same
shasum.update(delimiter)
shasum.update(content)
shasum.update(delimiter)
})
return shasum.digest('base64')
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment