Skip to content

Instantly share code, notes, and snippets.

@lhsfcboy
Last active August 25, 2017 17:43
Show Gist options
  • Save lhsfcboy/cba7659d303e1f90c007d06d1af31460 to your computer and use it in GitHub Desktop.
Save lhsfcboy/cba7659d303e1f90c007d06d1af31460 to your computer and use it in GitHub Desktop.
Google Script of Hash
function SHA256_BIN(input){
var binstr = '';
var digest = Utilities.computeDigest(Utilities.DigestAlgorithm.SHA_256, input);
for (i = 0; i < digest.length; i++) {
var val = (digest[i]+256) % 256;
var hexstring= ('0'+val.toString(16)).slice(-2);
var bytestring = Number('0x' + hexstring).toString(2);
padded_bytestring=String("00000000" + bytestring).slice(-8); // returns 00123
binstr += padded_bytestring;
}
return binstr;
}
function MD5 (input) {
var rawHash = Utilities.computeDigest(Utilities.DigestAlgorithm.MD5, input);
var txtHash = '';
for (i = 0; i < rawHash.length; i++) {
var hashVal = rawHash[i];
if (hashVal < 0) {
hashVal += 256;
}
if (hashVal.toString(16).length == 1) {
txtHash += '0';
}
txtHash += hashVal.toString(16);
}
return txtHash;
}
function SHA1(input) {
var rawHash = Utilities.computeDigest(Utilities.DigestAlgorithm.SHA_1, input);
var txtHash = '';
for (j = 0; j <rawHash.length; j++) {
var hashVal = rawHash[j];
if (hashVal < 0)
hashVal += 256;
if (hashVal.toString(16).length == 1)
txtHash += "0";
txtHash += hashVal.toString(16);
}
return txtHash;
}
function SHA256(s) {
var hexstr = '';
var digest = Utilities.computeDigest(Utilities.DigestAlgorithm.SHA_256, s);
for (i = 0; i < digest.length; i++) {
var val = (digest[i]+256) % 256;
hexstr += ('0'+val.toString(16)).slice(-2);
}
return hexstr;
}
function SHA384(s) {
var hexstr = '';
var digest = Utilities.computeDigest(Utilities.DigestAlgorithm.SHA_384, s);
for (i = 0; i < digest.length; i++) {
var val = (digest[i]+256) % 256;
hexstr += ('0'+val.toString(16)).slice(-2);
}
return hexstr;
}
function SHA512(s) {
var hexstr = '';
var digest = Utilities.computeDigest(Utilities.DigestAlgorithm.SHA_512, s);
for (i = 0; i < digest.length; i++) {
var val = (digest[i]+256) % 256;
hexstr += ('0'+val.toString(16)).slice(-2);
}
return hexstr;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment