Skip to content

Instantly share code, notes, and snippets.

@nikolayemrikh
Last active November 22, 2023 17:37
Show Gist options
  • Save nikolayemrikh/c8ee6b0bbeda0a2b65685844d503c6b9 to your computer and use it in GitHub Desktop.
Save nikolayemrikh/c8ee6b0bbeda0a2b65685844d503c6b9 to your computer and use it in GitHub Desktop.
Calculate SHA-1 hash GitHub API v3 way
git hash-object ./file
# or
git hash-object -t blob
# or
cat ./file | git hash-object --stdin
const crypto = require('crypto');
const getSHA1 = (data) => {
return crypto.createHash("sha1").update(data).digest("hex");
};
const getGitHubSHA = (data) => {
return getSHA1("blob " + Buffer.byteLength(data) + "\0" + data);
};
const str = `some text here`;
const sha = getGitHubSHA(str);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment