Skip to content

Instantly share code, notes, and snippets.

@nikolayemrikh
Last active January 3, 2025 12:37
Show Gist options
  • Select an option

  • Save nikolayemrikh/c8ee6b0bbeda0a2b65685844d503c6b9 to your computer and use it in GitHub Desktop.

Select an option

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);
@Stiff-Rock

Copy link
Copy Markdown

Does this work for binary files like an image?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment