Get SHA1 checksum of byte array in JavaScript? - javascript

So I need to get a SHA1 hash of a byte array in javascript (Will be array of integer values 0-255), I can't seem to figure out how to achive this.
I need to be able to get the same result as the C# SHA1.ComputeHash function, meaning I input a byte array and get a 20 byte array back representing the resulting checksum.
Does anyone have a way to achieve this?
Thanks!

Use the calcSHA1FromByte function to get the sha1 of a byte array
var SHA1Generator = {
hex_chr: "0123456789abcdef",
hex: function (num) {
var str = "";
for (var j = 7; j >= 0; j--)
str += this.hex_chr.charAt((num >> (j * 4)) & 0x0F);
return str;
},
str2blks_SHA1: function (str) {
var nblk = ((str.length + 8) >> 6) + 1;
var blks = new Array(nblk * 16);
for (var i = 0; i < nblk * 16; i++) blks[i] = 0;
for (i = 0; i < str.length; i++)
blks[i >> 2] |= str.charCodeAt(i) << (24 - (i % 4) * 8);
blks[i >> 2] |= 0x80 << (24 - (i % 4) * 8);
blks[nblk * 16 - 1] = str.length * 8;
return blks;
},
add: function (x, y) {
var lsw = (x & 0xFFFF) + (y & 0xFFFF);
var msw = (x >> 16) + (y >> 16) + (lsw >> 16);
return (msw << 16) | (lsw & 0xFFFF);
},
rol: function (num, cnt) {
return (num << cnt) | (num >>> (32 - cnt));
},
ft: function (t, b, c, d) {
if (t < 20) return (b & c) | ((~b) & d);
if (t < 40) return b ^ c ^ d;
if (t < 60) return (b & c) | (b & d) | (c & d);
return b ^ c ^ d;
},
kt: function (t) {
return (t < 20) ? 1518500249 : (t < 40) ? 1859775393 :
(t < 60) ? -1894007588 : -899497514;
},
calcSHA1FromByte: function(byteArr) {
var str = '';
for(var i=0; i<byteArr.length; i++)
str += String.fromCharCode(byteArr[i]);
return this.calcSHA1(str);
},
calcSHA1: function (str) {
if (str != '') {
var x = this.str2blks_SHA1(str);
var w = new Array(80);
var a = 1732584193;
var b = -271733879;
var c = -1732584194;
var d = 271733878;
var e = -1009589776;
for (var i = 0; i < x.length; i += 16) {
var olda = a;
var oldb = b;
var oldc = c;
var oldd = d;
var olde = e;
for (var j = 0; j < 80; j++) {
if (j < 16) w[j] = x[i + j];
else w[j] = this.rol(w[j - 3] ^ w[j - 8] ^ w[j - 14] ^ w[j - 16], 1);
t = this.add(this.add(this.rol(a, 5), this.ft(j, b, c, d)), this.add(this.add(e, w[j]), this.kt(j)));
e = d;
d = c;
c = this.rol(b, 30);
b = a;
a = t;
}
a = this.add(a, olda);
b = this.add(b, oldb);
c = this.add(c, oldc);
d = this.add(d, oldd);
e = this.add(e, olde);
}
return this.hex(a) + this.hex(b) + this.hex(c) + this.hex(d) + this.hex(e);
}
else {
return '';
}
}
};
// your byte array
var byteArr = ['100','101','114'];
var sha1 = SHA1Generator.calcSHA1FromByte(byteArr);

Related

TEA Encryption in Javascript

I am trying to convert Objective C TEA encryption to Javascript but inside one of the inner loops, numbers doesn't match against Objective C version
Here is the Objective C version
#define TIMES 32
#implementation NSData (NSData_extend)
// Encrypt NSData with pwdKey
-(NSData*)addTEA:(const unsigned int *)pwdkey
{
unsigned char * ch = (unsigned char*)[self bytes];
int n = 8-self.length%8;
char byte[self.length+n];
char cc = n;
for (int i=0; i<n; i++) {
if (i==0) {
byte[i] = cc;
}
else{
byte[i] = 0;
}
}
for (int i=0; i<self.length; i++) {
byte[n+i] = ch[i];
}
int delta = 0x9e3779b9;
int a = pwdkey[0];
int b = pwdkey[1];
int c = pwdkey[2];
int d = pwdkey[3];
unsigned char newbyte[self.length+n];
for (int offset=0; offset<self.length+n; offset += 8) {
int y = [self ByteTounint:byte[offset+3]] | [self ByteTounint:byte[offset+2]]<<8 | [self ByteTounint:byte[offset+1]]<<16 | [self ByteTounint:byte[offset+0]]<<24;
int z = [self ByteTounint:byte[offset+7]] | [self ByteTounint:byte[offset+6]]<<8 | [self ByteTounint:byte[offset+5]]<<16 | [self ByteTounint:byte[offset+4]]<<24;
int sum = 0;
for (int i=0; i<TIMES; i++) {
sum += delta;
y += ((z<<4) + a) ^ (z + sum) ^ ((z>>5) + b);
z += ((y<<4) + c) ^ (y + sum) ^ ((y>>5) + d);
}
newbyte[offset+7] = z & 0x000000ff;
newbyte[offset+6] = (z & 0x0000ff00) >> 8;
newbyte[offset+5] = (z & 0x00ff0000) >> 16;
newbyte[offset+4] = (z & 0xff000000) >> 24;
newbyte[offset+3] = y & 0x000000ff;
newbyte[offset+2] = (y & 0x0000ff00) >> 8;
newbyte[offset+1] = (y & 0x00ff0000) >> 16;
newbyte[offset+0] = (y & 0xff000000) >> 24;
}
NSData * resultData = [NSData dataWithBytes:newbyte length:self.length+n];
return resultData;
}
// Decrypt NSData with pwdKey
-(NSData*)subtractTEA:(const unsigned int *)pwdkey
{
unsigned char * byte = (unsigned char*)[self bytes];
int delta = 0x9e3779b9;
int a = pwdkey[0];
int b = pwdkey[1];
int c = pwdkey[2];
int d = pwdkey[3];
unsigned char newbyte[self.length];
for (int offset=0; offset<self.length; offset += 8) {
int y = [self ByteTounint:byte[offset+3]] | [self ByteTounint:byte[offset+2]]<<8 | [self ByteTounint:byte[offset+1]]<<16 | [self ByteTounint:byte[offset+0]]<<24;
int z = [self ByteTounint:byte[offset+7]] | [self ByteTounint:byte[offset+6]]<<8 | [self ByteTounint:byte[offset+5]]<<16 | [self ByteTounint:byte[offset+4]]<<24;
int sum = 0;
if (TIMES == 32) {
sum = 0xC6EF3720;
}
else if (TIMES == 16){
sum = 0xE3779B90;
}
else{
sum = delta * TIMES;
}
for (int i=0; i<TIMES; i++) {
z -= ((y<<4) + c) ^ (y + sum) ^ ((y>>5) + d);
y -= ((z<<4) + a) ^ (z + sum) ^ ((z>>5) + b);
sum -= delta;
}
newbyte[offset+7] = z & 0x000000ff;
newbyte[offset+6] = (z & 0x0000ff00) >> 8;
newbyte[offset+5] = (z & 0x00ff0000) >> 16;
newbyte[offset+4] = (z & 0xff000000) >> 24;
newbyte[offset+3] = y & 0x000000ff;
newbyte[offset+2] = (y & 0x0000ff00) >> 8;
newbyte[offset+1] = (y & 0x00ff0000) >> 16;
newbyte[offset+0] = (y & 0xff000000) >> 24;
}
int n = newbyte[0];
unsigned char ch[self.length-n];
for (int i=0; i<self.length-n; i++) {
ch[i] = newbyte[i+n];
}
NSData * resultData = [NSData dataWithBytes:ch length:self.length-n];
return resultData;
}
- (int)ByteTounint:(int)byte
{
if (byte<0) {
return (byte+256);
}
return byte;
}
My JavaScript version
TEA.prototype.encrypt = function(src,pwdkey) {
var TIMES = 32;
var n = 8 - (src.length % 8);
var byte = Buffer.alloc(src.length + n);
var cc = n;
for (var i = 0; i < n; i++) {
if (i == 0) {
byte[i] = cc;
}
else {
byte[i] = 0;
}
}
for (var j = 0; j < src.length; j++) {
byte.write( src[j],(n+j));
}
var delta = 0x9e3779b9;
var a = pwdkey.readInt32LE(0);
var b = pwdkey.readInt32LE(1);
var c = pwdkey.readInt32LE(2);
var d = pwdkey.readInt32LE(3);
var newbyte = Buffer.alloc(src.length + n);
for (var offset = 0; offset < src.length + n; offset += 8) {
var y = ByteTounint(byte[offset + 3]) | ByteTounint(byte[offset + 2] << 8) | ByteTounint(byte[offset + 1] << 16) | ByteTounint(byte[offset + 0]) << 24;
var z = ByteTounint(byte[offset + 7]) | ByteTounint(byte[offset + 6]) << 8 | ByteTounint(byte[offset + 5]) << 16 | ByteTounint(byte[offset + 4]) << 24;
var sum = 0;
for(var i=0;i<TIMES;i++)
{
sum += delta;
sum >>>= 0;
y += (((z<<4)+a) ^ (z+sum) ^ ((z>>>5)+b)) >>> 0;
z += (((y<<4)+c) ^ (y+sum) ^ ((y>>>5)+d)) >>> 0;
}
newbyte.writeInt8((z & 0x000000ff),(offset + 7));
newbyte.writeInt8(((z & 0x0000ff00) >> 8),(offset + 6));
newbyte.writeInt8(((z & 0x00ff0000) >> 16),(offset + 5));
newbyte.writeInt8(((z & 0xff000000) >> 24),(offset + 4));
newbyte.writeInt8((y & 0x000000ff),(offset + 3));
newbyte.writeInt8(((y & 0x0000ff00) >> 8),(offset + 2));
newbyte.writeInt8(((y & 0x00ff0000) >> 16),(offset + 1));
newbyte.writeInt8(((y & 0xff000000) >> 24),(offset + 0));
}
return newbyte
};
function ByteTounint(byte) {
if (byte<0) {
return (byte+256);
}
return byte;
}
Usage for JavaScript Version
var pwdkey = new Buffer("4523F10F214365873248738902EFCDAB","hex");
var dene = tea.encrypt("params={\"method\":\"waybill.querywaybill\",\"requstParams\":{\"memNo\":\"\",\"waybillNo\":\"606447740110\"}}",pwdkey);
Usage and Sample Result from Objective version
NSString *keyString = #"4523F10F214365873248738902EFCDAB";
NSData *keyData = [NSData dataWithHexString:keyString];
const unsigned char *src = (const unsigned char *)[key bytes];
NSString *str = #"params={\"method\":\"waybill.querywaybill\",\"requstParams\":{\"memNo\":\"\",\"waybillNo\":\"606447740110\"}}";
NSData *data = [NSData dataWithBytes:str.UTF8String length:str.length];
NSData * result2 = [data addTEA:src];
NSLog(#"%#",result2);
// prints 167da396 9b183f2e d12ac3f5 5083a581.....
My version starts to give wrong values inside for(var i=0;i<TIMES;i++) loop. Then it crashes during newbyte.writeInt8 part.
Finally, I made it work. Here is a working version which correctly encrypts and decrypts any size of text.
function ByteTounint(byte) {
if (byte<0) {
return (byte+256);
}
return byte;
}
TEA.prototype.decrypt = function(src,pwdkey) {
var TIMES = 32;
var delta = 0x9e3779b9;
var a = pwdkey.readUInt32LE(0);
var b = pwdkey.readUInt32LE(4);
var c = pwdkey.readUInt32LE(8);
var d = pwdkey.readUInt32LE(12);
var newbyte = Buffer.alloc(src.length);
for (var offset=0; offset<src.length; offset += 8) {
var y = ByteTounint(src[offset + 3]) | ByteTounint(src[offset + 2]) << 8 | ByteTounint(src[offset + 1]) << 16 | ByteTounint(src[offset + 0]) << 24;
var z = ByteTounint(src[offset + 7]) | ByteTounint(src[offset + 6]) << 8 | ByteTounint(src[offset + 5]) << 16 | ByteTounint(src[offset + 4]) << 24;
var sum = 0;
if (TIMES == 32) {
sum = 0xC6EF3720;
}
else if (TIMES == 16) {
sum = 0xE3779B90;
}
else {
sum = delta * TIMES;
}
for (var i = 0; i < TIMES; i++) {
z = (z - (( ( (y<<4) + c) & 0xFFFFFFFF) ^ ( (y + sum) & 0xFFFFFFFF ) ^ ( ((y >> 5) + d ) & 0xFFFFFFFF))) & 0xFFFFFFFF
y = (y - (( ( (z<<4) + a) & 0xFFFFFFFF) ^ ( (z + sum) & 0xFFFFFFFF ) ^ ( ((z >> 5) + b ) & 0xFFFFFFFF))) & 0xFFFFFFFF
sum = (sum - delta) & 0xFFFFFFFF;
}
newbyte.writeInt32BE((y & 0xFFFFFFFF),offset);
newbyte.writeInt32BE((z & 0xFFFFFFFF),(offset+4));
}
var n = newbyte[0];
var ch = Buffer.alloc(src.length - n);
for (var i=0; i<src.length-n; i++) {
ch[i] = newbyte[i+n];
}
return ch;
};
TEA.prototype.encrypt = function(src,pwdkey) {
var TIMES = 32;
var n = 8 - (src.length % 8);
var byte = Buffer.alloc(src.length + n);
var cc = n;
for (var i = 0; i < n; i++) {
if (i == 0) {
byte[i] = cc;
}
else {
byte[i] = 0;
}
}
for (var j = 0; j < src.length; j++) {
byte.write( src[j],(n+j));
}
var delta = 0x9e3779b9;
var a = pwdkey.readUInt32LE(0);
var b = pwdkey.readUInt32LE(4);
var c = pwdkey.readUInt32LE(8);
var d = pwdkey.readUInt32LE(12);
var newbyte = Buffer.alloc(src.length + n);
for (var offset = 0; offset < src.length + n; offset += 8) {
var y = ByteTounint(byte[offset + 3]) | ByteTounint(byte[offset + 2]) << 8 | ByteTounint(byte[offset + 1]) << 16 | ByteTounint(byte[offset + 0]) << 24;
var z = ByteTounint(byte[offset + 7]) | ByteTounint(byte[offset + 6]) << 8 | ByteTounint(byte[offset + 5]) << 16 | ByteTounint(byte[offset + 4]) << 24;
var sum = 0;
for(var i=0;i<TIMES;i++)
{
sum = (sum + delta) & 0xFFFFFFFF;
y = (y + (( ( (z<<4) + a) & 0xFFFFFFFF) ^ ( (z + sum) & 0xFFFFFFFF ) ^ ( ((z >> 5) + b ) & 0xFFFFFFFF))) & 0xFFFFFFFF;
z = (z + (( ( (y<<4) + c) & 0xFFFFFFFF) ^ ( (y + sum) & 0xFFFFFFFF ) ^ ( ((y >> 5) + d ) & 0xFFFFFFFF))) & 0xFFFFFFFF
}
newbyte.writeInt32BE((y & 0xFFFFFFFF),offset);
newbyte.writeInt32BE((z & 0xFFFFFFFF),(offset+4));
}
return newbyte
};
```

Generating the same SHA1 UUID in golang and Javascript

I have what I thought was a pretty simply question. I'm using this code to generate a SHA1 uuid in Golang:
namespace := uuid.Parse("b9cfdb9d-f741-4e1f-89ae-fac6b2a5d740")
sha := uuid.NewSHA1(namespace, []byte("something"))
fmt.Println(sha.String())
Now I want to generate the same UUID in javascript, and I thought it would be as easy as something like this:
var hash = CryptoJS.SHA1("b9cfdb9d-f741-4e1f-89ae-fac6b2a5d740" + "something")
// chomp the hash into a UUID string
However, I'm running into serious issues. It seems that the uuid.Parse function in Golang is running this parsing function that converts the namespace to a 16-byte array, so even though I use the same SHA1 algorithm in Javascript, I'm not getting the same output.
I'v been messing around with doing the same in JS, but I'm stumped.
Any smart crypto people in here that can help me?
Well, that only took me a month.
var SHA1Generator = {
hex_chr: "0123456789abcdef",
hex: function (num) {
var str = "";
for (var j = 7; j >= 0; j--)
str += this.hex_chr.charAt((num >> (j * 4)) & 0x0F);
return str;
},
str2blks_SHA1: function (str) {
var nblk = ((str.length + 8) >> 6) + 1;
var blks = new Array(nblk * 16);
for (var i = 0; i < nblk * 16; i++) blks[i] = 0;
for (i = 0; i < str.length; i++)
blks[i >> 2] |= str.charCodeAt(i) << (24 - (i % 4) * 8);
blks[i >> 2] |= 0x80 << (24 - (i % 4) * 8);
blks[nblk * 16 - 1] = str.length * 8;
return blks;
},
add: function (x, y) {
var lsw = (x & 0xFFFF) + (y & 0xFFFF);
var msw = (x >> 16) + (y >> 16) + (lsw >> 16);
return (msw << 16) | (lsw & 0xFFFF);
},
rol: function (num, cnt) {
return (num << cnt) | (num >>> (32 - cnt));
},
ft: function (t, b, c, d) {
if (t < 20) return (b & c) | ((~b) & d);
if (t < 40) return b ^ c ^ d;
if (t < 60) return (b & c) | (b & d) | (c & d);
return b ^ c ^ d;
},
kt: function (t) {
return (t < 20) ? 1518500249 : (t < 40) ? 1859775393 :
(t < 60) ? -1894007588 : -899497514;
},
calcSHA1FromByte: function(byteArr) {
var str = '';
for(var i=0; i<byteArr.length; i++)
str += String.fromCharCode(byteArr[i]);
return this.calcSHA1(str);
},
calcSHA1: function (str) {
if (str != '') {
var x = this.str2blks_SHA1(str);
var w = new Array(80);
var a = 1732584193;
var b = -271733879;
var c = -1732584194;
var d = 271733878;
var e = -1009589776;
for (var i = 0; i < x.length; i += 16) {
var olda = a;
var oldb = b;
var oldc = c;
var oldd = d;
var olde = e;
for (var j = 0; j < 80; j++) {
if (j < 16) w[j] = x[i + j];
else w[j] = this.rol(w[j - 3] ^ w[j - 8] ^ w[j - 14] ^ w[j - 16], 1);
t = this.add(this.add(this.rol(a, 5), this.ft(j, b, c, d)), this.add(this.add(e, w[j]), this.kt(j)));
e = d;
d = c;
c = this.rol(b, 30);
b = a;
a = t;
}
a = this.add(a, olda);
b = this.add(b, oldb);
c = this.add(c, oldc);
d = this.add(d, oldd);
e = this.add(e, olde);
}
return this.hex(a) + this.hex(b) + this.hex(c) + this.hex(d) + this.hex(e);
}
else {
return '';
}
}
};
function stringToByteArray(str) {
var bytes = [];
for (var i = 0; i < str.length; ++i) {
bytes.push(str.charCodeAt(i));
}
return bytes;
}
function uuidToByteArray(hex) {
// If this is a uuid, remove the dashes
hex = hex.replace(/-/g, "");
// convert each hex number into a string representation
// of the byte integer.
var bytes = [];
for(var i = 0; i < hex.length; i += 2) {
bytes.push(parseInt(hex.substring(i, i+2),16));
}
return bytes;
}
function sha1ToUUID5(hash) {
var uuid = hash.substring(0, 8) +
'-' + hash.substring(8, 12) +
// four most significant bits holds version number 5
'-' + ((parseInt(hash.substring(12, 16), 16) & 0x0fff) | 0x5000).toString(16) +
// two most significant bits holds zero and one for variant DCE1.1
'-' + ((parseInt(hash.substring(16, 20), 16) & 0x3fff) | 0x8000).toString(16) +
'-' + hash.substring(20, 32); //12 digits
return uuid;
}
var namespace = "e75a36a9-3323-40dd-a7d1-1c57ad2aa3cd"
var id = "event154"
var namespaceBytes = uuidToByteArray(namespace);
var idBytes = stringToByteArray(id);
var allBytes = namespaceBytes.concat(idBytes);
console.log("ORG 4505612c-c323-5d6f-b5cc-b7f362b9ba55")
console.log("NEW " + sha1ToUUID5(SHA1Generator.calcSHA1FromByte(allBytes)))

Need Sha1 encryption in jquery/javascript

I have sha1 encryption coding in php
<?php
echo hash('sha1', 'testing'.'abcdb');
?>
but, my requirement should have to run the page xyz.html, so the above block is not working.
So, I need to do sha1 encryption in jquery/javascript. Help me by providing coding for sha1 encryption.
this is exactly have to be converted to scripts
'<?php echo hash('sha1', 'my-secret-key' . $_REQUEST["email"]); ?>'
Just google it and found: http://phpjs.org/functions/sha1/
function sha1(str) {
// discuss at: http://phpjs.org/functions/sha1/
// original by: Webtoolkit.info (http://www.webtoolkit.info/)
// improved by: Michael White (http://getsprink.com)
// improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
// input by: Brett Zamir (http://brett-zamir.me)
// depends on: utf8_encode
// example 1: sha1('Kevin van Zonneveld');
// returns 1: '54916d2e62f65b3afa6e192e6a601cdbe5cb5897'
var rotate_left = function(n, s) {
var t4 = (n << s) | (n >>> (32 - s));
return t4;
};
/*var lsb_hex = function (val) { // Not in use; needed?
var str="";
var i;
var vh;
var vl;
for ( i=0; i<=6; i+=2 ) {
vh = (val>>>(i*4+4))&0x0f;
vl = (val>>>(i*4))&0x0f;
str += vh.toString(16) + vl.toString(16);
}
return str;
};*/
var cvt_hex = function(val) {
var str = '';
var i;
var v;
for (i = 7; i >= 0; i--) {
v = (val >>> (i * 4)) & 0x0f;
str += v.toString(16);
}
return str;
};
var blockstart;
var i, j;
var W = new Array(80);
var H0 = 0x67452301;
var H1 = 0xEFCDAB89;
var H2 = 0x98BADCFE;
var H3 = 0x10325476;
var H4 = 0xC3D2E1F0;
var A, B, C, D, E;
var temp;
str = this.utf8_encode(str);
var str_len = str.length;
var word_array = [];
for (i = 0; i < str_len - 3; i += 4) {
j = str.charCodeAt(i) << 24 | str.charCodeAt(i + 1) << 16 | str.charCodeAt(i + 2) << 8 | str.charCodeAt(i + 3);
word_array.push(j);
}
switch (str_len % 4) {
case 0:
i = 0x080000000;
break;
case 1:
i = str.charCodeAt(str_len - 1) << 24 | 0x0800000;
break;
case 2:
i = str.charCodeAt(str_len - 2) << 24 | str.charCodeAt(str_len - 1) << 16 | 0x08000;
break;
case 3:
i = str.charCodeAt(str_len - 3) << 24 | str.charCodeAt(str_len - 2) << 16 | str.charCodeAt(str_len - 1) <<
8 | 0x80;
break;
}
word_array.push(i);
while ((word_array.length % 16) != 14) {
word_array.push(0);
}
word_array.push(str_len >>> 29);
word_array.push((str_len << 3) & 0x0ffffffff);
for (blockstart = 0; blockstart < word_array.length; blockstart += 16) {
for (i = 0; i < 16; i++) {
W[i] = word_array[blockstart + i];
}
for (i = 16; i <= 79; i++) {
W[i] = rotate_left(W[i - 3] ^ W[i - 8] ^ W[i - 14] ^ W[i - 16], 1);
}
A = H0;
B = H1;
C = H2;
D = H3;
E = H4;
for (i = 0; i <= 19; i++) {
temp = (rotate_left(A, 5) + ((B & C) | (~B & D)) + E + W[i] + 0x5A827999) & 0x0ffffffff;
E = D;
D = C;
C = rotate_left(B, 30);
B = A;
A = temp;
}
for (i = 20; i <= 39; i++) {
temp = (rotate_left(A, 5) + (B ^ C ^ D) + E + W[i] + 0x6ED9EBA1) & 0x0ffffffff;
E = D;
D = C;
C = rotate_left(B, 30);
B = A;
A = temp;
}
for (i = 40; i <= 59; i++) {
temp = (rotate_left(A, 5) + ((B & C) | (B & D) | (C & D)) + E + W[i] + 0x8F1BBCDC) & 0x0ffffffff;
E = D;
D = C;
C = rotate_left(B, 30);
B = A;
A = temp;
}
for (i = 60; i <= 79; i++) {
temp = (rotate_left(A, 5) + (B ^ C ^ D) + E + W[i] + 0xCA62C1D6) & 0x0ffffffff;
E = D;
D = C;
C = rotate_left(B, 30);
B = A;
A = temp;
}
H0 = (H0 + A) & 0x0ffffffff;
H1 = (H1 + B) & 0x0ffffffff;
H2 = (H2 + C) & 0x0ffffffff;
H3 = (H3 + D) & 0x0ffffffff;
H4 = (H4 + E) & 0x0ffffffff;
}
temp = cvt_hex(H0) + cvt_hex(H1) + cvt_hex(H2) + cvt_hex(H3) + cvt_hex(H4);
return temp.toLowerCase();
}
call
sha1('testing'+'abcdb');
There are two ways to go about this, you can either use AJAX to call up your PHP page and retrieve the SHA-1.
Or, you could use one of the JS libraries, such as CryptoJS.

Javascript using HMAC SHA256 with base64 encoded key

I am using the following function to create a base64 HMAC SHA256 hash
https://gist.github.com/hanih/7443134
function sha256() {
var sha256;
sha256 = "undefined" !== typeof exports ? exports : {};
sha256.hexcase = 0;
sha256.b64pad = "=";
var b64pad = "=";
var hexcase = 0;
sha256.hex_sha256 = function(a) {
return sha256.rstr2hex(sha256.rstr_sha256(sha256.str2rstr_utf8(a)));
};
sha256.b64_sha256 = function(a) {
return sha256.rstr_sha256(sha256.str2rstr_utf8(a));
};
sha256.any_sha256 = function(a, c) {
return sha256.rstr2any(sha256.rstr_sha256(sha256.str2rstr_utf8(a)), c);
};
sha256.hex_hmac_sha256 = function(a, c) {
return sha256.rstr2hex(sha256.rstr_hmac_sha256(sha256.str2rstr_utf8(a), sha256.str2rstr_utf8(c)));
};
sha256.b64_hmac_sha256 = function(a, c) {
return sha256.rstr2b64(sha256.rstr_hmac_sha256(sha256.str2rstr_utf8(a), sha256.str2rstr_utf8(c)));
};
sha256.b64_hmac_sha256_sha256 = function(a, c) {
return sha256.rstr2b64(sha256.rstr_hmac_sha256(a, sha256.rstr_sha256(c)));
};
sha256.any_hmac_sha256 = function(a, c, b) {
return sha256.rstr2any(sha256.rstr_hmac_sha256(sha256.str2rstr_utf8(a), sha256.str2rstr_utf8(c)), b);
};
sha256.sha256_vm_test = function() {
return "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad" == sha256.hex_sha256("abc").toLowerCase();
};
sha256.sha256_vm_test1 = function() {
return "d7a8fbb307d7809469ca9abcb0082e4f8d5651e46d3cdb762d02d0bf37c9e592" == sha256.hex_sha256("The quick brown fox jumps over the lazy dog").toLowerCase();
};
sha256.rstr_sha256 = function(a) {
return sha256.binb2rstr(sha256.binb_sha256(sha256.rstr2binb(a), 8 * a.length));
};
sha256.rstr_hmac_sha256 = function(a, c) {
var b = sha256.rstr2binb(a);
16 < b.length && ( b = sha256.binb_sha256(b, 8 * a.length));
for (var d = Array(16), e = Array(16), f = 0; 16 > f; f++)
d[f] = b[f] ^ 909522486, e[f] = b[f] ^ 1549556828;
b = sha256.binb_sha256(d.concat(sha256.rstr2binb(c)), 512 + 8 * c.length);
return sha256.binb2rstr(sha256.binb_sha256(e.concat(b), 768));
};
sha256.rstr2hex = function(a) {
try { hexcase;
} catch(c) {
hexcase = 0;
}
for (var b = hexcase ? "0123456789ABCDEF" : "0123456789abcdef", d = "", e, f = 0; f < a.length; f++)
e = a.charCodeAt(f), d += b.charAt(e >>> 4 & 15) + b.charAt(e & 15);
return d;
};
sha256.rstr2b64 = function(a) {
try { b64pad;
} catch(c) {
b64pad = "";
}
for (var b = "", d = a.length, e = 0; e < d; e += 3)
for (var f = a.charCodeAt(e) << 16 | (e + 1 < d ? a.charCodeAt(e + 1) << 8 : 0) | (e + 2 < d ? a.charCodeAt(e + 2) : 0), g = 0; 4 > g; g++)
b = 8 * e + 6 * g > 8 * a.length ? b + b64pad : b + "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".charAt(f >>> 6 * (3 - g) & 63);
return b;
};
sha256.rstr2any = function(a, c) {
var b = c.length, d = [], e, f, g, h, j = Array(Math.ceil(a.length / 2));
for ( e = 0; e < j.length; e++)
j[e] = a.charCodeAt(2 * e) << 8 | a.charCodeAt(2 * e + 1);
for (; 0 < j.length; ) {
h = [];
for ( e = g = 0; e < j.length; e++)
if ( g = (g << 16) + j[e], f = Math.floor(g / b), g -= f * b, 0 < h.length || 0 < f)
h[h.length] = f;
d[d.length] = g;
j = h;
}
b = "";
for ( e = d.length - 1; 0 <= e; e--)
b += c.charAt(d[e]);
d = Math.ceil(8 * a.length / (Math.log(c.length) / Math.log(2)));
for ( e = b.length; e < d; e++)
b = c[0] + b;
return b;
};
sha256.str2rstr_utf8 = function(a) {
for (var c = "", b = -1, d, e; ++b < a.length; )
d = a.charCodeAt(b), e = b + 1 < a.length ? a.charCodeAt(b + 1) : 0, 55296 <= d && 56319 >= d && 56320 <= e && 57343 >= e && ( d = 65536 + ((d & 1023) << 10) + (e & 1023), b++), 127 >= d ? c += String.fromCharCode(d) : 2047 >= d ? c += String.fromCharCode(192 | d >>> 6 & 31, 128 | d & 63) : 65535 >= d ? c += String.fromCharCode(224 | d >>> 12 & 15, 128 | d >>> 6 & 63, 128 | d & 63) : 2097151 >= d && (c += String.fromCharCode(240 | d >>> 18 & 7, 128 | d >>> 12 & 63, 128 | d >>> 6 & 63, 128 | d & 63));
return c;
};
sha256.str2rstr_utf16le = function(a) {
for (var c = "", b = 0; b < a.length; b++)
c += String.fromCharCode(a.charCodeAt(b) & 255, a.charCodeAt(b) >>> 8 & 255);
return c;
};
str2rstr_utf16be = function(a) {
for (var c = "", b = 0; b < a.length; b++)
c += String.fromCharCode(a.charCodeAt(b) >>> 8 & 255, a.charCodeAt(b) & 255);
return c;
};
sha256.rstr2binb = function(a) {
for (var c = Array(a.length >> 2), b = 0; b < c.length; b++)
c[b] = 0;
for ( b = 0; b < 8 * a.length; b += 8)
c[b >> 5] |= (a.charCodeAt(b / 8) & 255) << 24 - b % 32;
return c;
};
sha256.binb2rstr = function(a) {
for (var c = "", b = 0; b < 32 * a.length; b += 8)
c += String.fromCharCode(a[b >> 5] >>> 24 - b % 32 & 255);
return c;
};
sha256.sha256_S = function(a, c) {
return a >>> c | a << 32 - c;
};
sha256.sha256_R = function(a, c) {
return a >>> c;
};
sha256.sha256_Ch = function(a, c, b) {
return a & c ^ ~a & b;
};
sha256.sha256_Maj = function(a, c, b) {
return a & c ^ a & b ^ c & b;
};
sha256.sha256_Sigma0256 = function(a) {
return sha256.sha256_S(a, 2) ^ sha256.sha256_S(a, 13) ^ sha256.sha256_S(a, 22);
};
sha256.sha256_Sigma1256 = function(a) {
return sha256.sha256_S(a, 6) ^ sha256.sha256_S(a, 11) ^ sha256.sha256_S(a, 25);
};
sha256.sha256_Gamma0256 = function(a) {
return sha256.sha256_S(a, 7) ^ sha256.sha256_S(a, 18) ^ sha256.sha256_R(a, 3);
};
sha256.sha256_Gamma1256 = function(a) {
return sha256.sha256_S(a, 17) ^ sha256.sha256_S(a, 19) ^ sha256.sha256_R(a, 10);
};
sha256.sha256_Sigma0512 = function(a) {
return sha256.sha256_S(a, 28) ^ sha256.sha256_S(a, 34) ^ sha256.sha256_S(a, 39);
};
sha256.sha256_Sigma1512 = function(a) {
return sha256.sha256_S(a, 14) ^ sha256.sha256_S(a, 18) ^ sha256.sha256_S(a, 41);
};
sha256.sha256_Gamma0512 = function(a) {
return sha256.sha256_S(a, 1) ^ sha256.sha256_S(a, 8) ^ sha256.sha256_R(a, 7);
};
sha256.sha256_Gamma1512 = function(a) {
return sha256.sha256_S(a, 19) ^ sha256.sha256_S(a, 61) ^ sha256.sha256_R(a, 6);
};
sha256.sha256_K = [1116352408, 1899447441, -1245643825, -373957723, 961987163, 1508970993, -1841331548, -1424204075, -670586216, 310598401, 607225278, 1426881987, 1925078388, -2132889090, -1680079193, -1046744716, -459576895, -272742522, 264347078, 604807628, 770255983, 1249150122, 1555081692, 1996064986, -1740746414, -1473132947, -1341970488, -1084653625, -958395405, -710438585, 113926993, 338241895, 666307205, 773529912, 1294757372, 1396182291, 1695183700, 1986661051, -2117940946, -1838011259, -1564481375, -1474664885, -1035236496, -949202525, -778901479, -694614492, -200395387, 275423344, 430227734, 506948616, 659060556, 883997877, 958139571, 1322822218, 1537002063, 1747873779, 1955562222, 2024104815, -2067236844, -1933114872, -1866530822, -1538233109, -1090935817, -965641998];
sha256.binb_sha256 = function(a, c) {
var b = [1779033703, -1150833019, 1013904242, -1521486534, 1359893119, -1694144372, 528734635, 1541459225], d = Array(64), e, f, g, h, j, k, m, o, p, n, l, q;
a[c >> 5] |= 128 << 24 - c % 32;
a[(c + 64 >> 9 << 4) + 15] = c;
for ( p = 0; p < a.length; p += 16) {
e = b[0];
f = b[1];
g = b[2];
h = b[3];
j = b[4];
k = b[5];
m = b[6];
o = b[7];
for ( n = 0; 64 > n; n++)
d[n] = 16 > n ? a[n + p] : sha256.safe_add(sha256.safe_add(sha256.safe_add(sha256.sha256_Gamma1256(d[n - 2]), d[n - 7]), sha256.sha256_Gamma0256(d[n - 15])), d[n - 16]), l = sha256.safe_add(sha256.safe_add(sha256.safe_add(sha256.safe_add(o, sha256.sha256_Sigma1256(j)), sha256.sha256_Ch(j, k, m)), sha256.sha256_K[n]), d[n]), q = sha256.safe_add(sha256.sha256_Sigma0256(e), sha256.sha256_Maj(e, f, g)), o = m, m = k, k = j, j = sha256.safe_add(h, l), h = g, g = f, f = e, e = sha256.safe_add(l, q);
b[0] = sha256.safe_add(e, b[0]);
b[1] = sha256.safe_add(f, b[1]);
b[2] = sha256.safe_add(g, b[2]);
b[3] = sha256.safe_add(h, b[3]);
b[4] = sha256.safe_add(j, b[4]);
b[5] = sha256.safe_add(k, b[5]);
b[6] = sha256.safe_add(m, b[6]);
b[7] = sha256.safe_add(o, b[7]);
}
return b;
};
sha256.safe_add = function(a, c) {
var b = (a & 65535) + (c & 65535);
return (a >> 16) + (c >> 16) + (b >> 16) << 16 | b & 65535;
};
return sha256;
}
module.exports = sha256;
it contains a function named b64_hmac_sha256 that works fine.
I use a base 64 encoded key that I decode using the following function:
https://gist.github.com/hanih/7443203
function urlDecode(str){
str=str.replace(new RegExp('\\+','g'),' ');
return unescape(str);
}
function urlEncode(str){
str=escape(str);
str=str.replace(new RegExp('\\+','g'),'%2B');
return str.replace(new RegExp('%20','g'),'+');
}
var END_OF_INPUT = -1;
var base64Chars = new Array(
'A','B','C','D','E','F','G','H',
'I','J','K','L','M','N','O','P',
'Q','R','S','T','U','V','W','X',
'Y','Z','a','b','c','d','e','f',
'g','h','i','j','k','l','m','n',
'o','p','q','r','s','t','u','v',
'w','x','y','z','0','1','2','3',
'4','5','6','7','8','9','+','/'
);
var reverseBase64Chars = new Array();
for (var i=0; i < base64Chars.length; i++){
reverseBase64Chars[base64Chars[i]] = i;
}
var base64Str;
var base64Count;
function setBase64Str(str){
base64Str = str;
base64Count = 0;
}
function readBase64(){
if (!base64Str) return END_OF_INPUT;
if (base64Count >= base64Str.length) return END_OF_INPUT;
var c = base64Str.charCodeAt(base64Count) & 0xff;
base64Count++;
return c;
}
function encodeBase64(str){
setBase64Str(str);
var result = '';
var inBuffer = new Array(3);
var lineCount = 0;
var done = false;
while (!done && (inBuffer[0] = readBase64()) != END_OF_INPUT){
inBuffer[1] = readBase64();
inBuffer[2] = readBase64();
result += (base64Chars[ inBuffer[0] >> 2 ]);
if (inBuffer[1] != END_OF_INPUT){
result += (base64Chars [(( inBuffer[0] << 4 ) & 0x30) | (inBuffer[1] >> 4) ]);
if (inBuffer[2] != END_OF_INPUT){
result += (base64Chars [((inBuffer[1] << 2) & 0x3c) | (inBuffer[2] >> 6) ]);
result += (base64Chars [inBuffer[2] & 0x3F]);
} else {
result += (base64Chars [((inBuffer[1] << 2) & 0x3c)]);
result += ('=');
done = true;
}
} else {
result += (base64Chars [(( inBuffer[0] << 4 ) & 0x30)]);
result += ('=');
result += ('=');
done = true;
}
lineCount += 4;
if (lineCount >= 76){
result += ('\n');
lineCount = 0;
}
}
return result;
}
function readReverseBase64(){
if (!base64Str) return END_OF_INPUT;
while (true){
if (base64Count >= base64Str.length) return END_OF_INPUT;
var nextCharacter = base64Str.charAt(base64Count);
base64Count++;
if (reverseBase64Chars[nextCharacter]){
return reverseBase64Chars[nextCharacter];
}
if (nextCharacter == 'A') return 0;
}
return END_OF_INPUT;
}
function ntos(n){
n=n.toString(16);
if (n.length == 1) n="0"+n;
n="%"+n;
return unescape(n);
}
function decodeBase64(str){
setBase64Str(str);
var result = "";
var inBuffer = new Array(4);
var done = false;
while (!done && (inBuffer[0] = readReverseBase64()) != END_OF_INPUT
&& (inBuffer[1] = readReverseBase64()) != END_OF_INPUT){
inBuffer[2] = readReverseBase64();
inBuffer[3] = readReverseBase64();
result += ntos((((inBuffer[0] << 2) & 0xff)| inBuffer[1] >> 4));
if (inBuffer[2] != END_OF_INPUT){
result += ntos((((inBuffer[1] << 4) & 0xff)| inBuffer[2] >> 2));
if (inBuffer[3] != END_OF_INPUT){
result += ntos((((inBuffer[2] << 6) & 0xff) | inBuffer[3]));
} else {
done = true;
}
} else {
done = true;
}
}
return result;
}
var digitArray = new Array('0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f');
function toHex(n){
var result = ''
var start = true;
for (var i=32; i>0;){
i-=4;
var digit = (n>>i) & 0xf;
if (!start || digit != 0){
start = false;
result += digitArray[digit];
}
}
return (result==''?'0':result);
}
function pad(str, len, pad){
var result = str;
for (var i=str.length; i<len; i++){
result = pad + result;
}
return result;
}
function encodeHex(str){
var result = "";
for (var i=0; i<str.length; i++){
result += pad(toHex(str.charCodeAt(i)&0xff),2,'0');
}
return result;
}
var hexv = {
"00":0,"01":1,"02":2,"03":3,"04":4,"05":5,"06":6,"07":7,"08":8,"09":9,"0A":10,"0B":11,"0C":12,"0D":13,"0E":14,"0F":15,
"10":16,"11":17,"12":18,"13":19,"14":20,"15":21,"16":22,"17":23,"18":24,"19":25,"1A":26,"1B":27,"1C":28,"1D":29,"1E":30,"1F":31,
"20":32,"21":33,"22":34,"23":35,"24":36,"25":37,"26":38,"27":39,"28":40,"29":41,"2A":42,"2B":43,"2C":44,"2D":45,"2E":46,"2F":47,
"30":48,"31":49,"32":50,"33":51,"34":52,"35":53,"36":54,"37":55,"38":56,"39":57,"3A":58,"3B":59,"3C":60,"3D":61,"3E":62,"3F":63,
"40":64,"41":65,"42":66,"43":67,"44":68,"45":69,"46":70,"47":71,"48":72,"49":73,"4A":74,"4B":75,"4C":76,"4D":77,"4E":78,"4F":79,
"50":80,"51":81,"52":82,"53":83,"54":84,"55":85,"56":86,"57":87,"58":88,"59":89,"5A":90,"5B":91,"5C":92,"5D":93,"5E":94,"5F":95,
"60":96,"61":97,"62":98,"63":99,"64":100,"65":101,"66":102,"67":103,"68":104,"69":105,"6A":106,"6B":107,"6C":108,"6D":109,"6E":110,"6F":111,
"70":112,"71":113,"72":114,"73":115,"74":116,"75":117,"76":118,"77":119,"78":120,"79":121,"7A":122,"7B":123,"7C":124,"7D":125,"7E":126,"7F":127,
"80":128,"81":129,"82":130,"83":131,"84":132,"85":133,"86":134,"87":135,"88":136,"89":137,"8A":138,"8B":139,"8C":140,"8D":141,"8E":142,"8F":143,
"90":144,"91":145,"92":146,"93":147,"94":148,"95":149,"96":150,"97":151,"98":152,"99":153,"9A":154,"9B":155,"9C":156,"9D":157,"9E":158,"9F":159,
"A0":160,"A1":161,"A2":162,"A3":163,"A4":164,"A5":165,"A6":166,"A7":167,"A8":168,"A9":169,"AA":170,"AB":171,"AC":172,"AD":173,"AE":174,"AF":175,
"B0":176,"B1":177,"B2":178,"B3":179,"B4":180,"B5":181,"B6":182,"B7":183,"B8":184,"B9":185,"BA":186,"BB":187,"BC":188,"BD":189,"BE":190,"BF":191,
"C0":192,"C1":193,"C2":194,"C3":195,"C4":196,"C5":197,"C6":198,"C7":199,"C8":200,"C9":201,"CA":202,"CB":203,"CC":204,"CD":205,"CE":206,"CF":207,
"D0":208,"D1":209,"D2":210,"D3":211,"D4":212,"D5":213,"D6":214,"D7":215,"D8":216,"D9":217,"DA":218,"DB":219,"DC":220,"DD":221,"DE":222,"DF":223,
"E0":224,"E1":225,"E2":226,"E3":227,"E4":228,"E5":229,"E6":230,"E7":231,"E8":232,"E9":233,"EA":234,"EB":235,"EC":236,"ED":237,"EE":238,"EF":239,
"F0":240,"F1":241,"F2":242,"F3":243,"F4":244,"F5":245,"F6":246,"F7":247,"F8":248,"F9":249,"FA":250,"FB":251,"FC":252,"FD":253,"FE":254,"FF":255
};
function decodeHex(str){
str = str.toUpperCase().replace(new RegExp("s/[^0-9A-Z]//g"));
var result = "";
var nextchar = "";
for (var i=0; i<str.length; i++){
nextchar += str.charAt(i);
if (nextchar.length == 2){
result += ntos(hexv[nextchar]);
nextchar = "";
}
}
return result;
}
The problem happens when the decoded key contains uncommon characters
This encoded key works:
dGhpc2lzYWxvbmdlcm1lc3NhZ2VvZmNvdXJzZXRoaXNpc2Fsb25nZXJtZXNzYWdlb2Zjb3Vyc2U=
but this doesn't work
5VoyMfmtN7lBiFlyDcMX85Hjvw/oxj8IVcB0dn8N6CXr+F0nuPI2LQ3K/w==

sha1 function in Javascript to python [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
How to convert the following js to python?
function sha1(str1,raw)
{
var hexcase = 0;
var chrsz = 8;
str1 = utf16to8(str1);
function utf16to8(str)
{
var out, i, len, c;
out = "";
len = str.length;
for(i = 0; i < len; i++)
{
c = str.charCodeAt(i);
if ((c >= 0x0001) && (c <= 0x007F))
{
out += str.charAt(i);
}
else if (c > 0x07FF)
{
out += String.fromCharCode(0xE0 | ((c >> 12) & 0x0F));
out += String.fromCharCode(0x80 | ((c >> 6) & 0x3F));
out += String.fromCharCode(0x80 | ((c >> 0) & 0x3F));
}
else
{
out += String.fromCharCode(0xC0 | ((c >> 6) & 0x1F));
out += String.fromCharCode(0x80 | ((c >> 0) & 0x3F));
}
}
return out;
}
function hex_sha1(s)
{
return binb2hex(core_sha1(str2binb(s),s.length * chrsz));
}
function str_sha1(s)
{
return binb2str(core_sha1(str2binb(s),s.length * chrsz));
}
function binb2hex(binarray)
{
var hex_tab = hexcase ? "0123456789ABCDEF" : "0123456789abcdef";
var str = "";
for(var i = 0; i < binarray.length * 4; i++)
{
str += hex_tab.charAt((binarray[i>>2] >> ((3 - i%4)*8+4)) & 0xF) +
hex_tab.charAt((binarray[i>>2] >> ((3 - i%4)*8 )) & 0xF);
}
return str;
}
function binb2str(bin)
{
var str = "";
var mask = (1 << chrsz) - 1;
for(var i = 0; i < bin.length * 32; i += chrsz)
str += String.fromCharCode((bin[i>>5] >>> (32 - chrsz - i%32)) & mask);
return str;
}
function str2binb(str2)
{
var bin = Array();
var mask = (1 << chrsz) - 1;
for(var i = 0; i < str2.length * chrsz; i += chrsz)
bin[i>>5] |= (str2.charCodeAt(i / chrsz) & mask) << (32 - chrsz - i%32);
return bin;
}
function safe_add(x, y)
{
var lsw = (x & 0xFFFF) + (y & 0xFFFF);
var msw = (x >> 16) + (y >> 16) + (lsw >> 16);
return (msw << 16) | (lsw & 0xFFFF);
}
function rol(num, cnt)
{
return (num << cnt) | (num >>> (32 - cnt));
}
function sha1_ft(t, b, c, d)
{
if(t < 20)
return (b & c) | ((~b) & d);
if(t < 40)
return b ^ c ^ d;
if(t < 60)
return (b & c) | (b & d) | (c & d);
return b ^ c ^ d;
}
function sha1_kt(t)
{
return (t < 20) ? 1518500249 : (t < 40) ? 1859775393 : (t < 60) ? -1894007588 : -899497514;
}
function core_sha1(x, len)
{
x[len >> 5] |= 0x80 << (24 - len % 32);
x[((len + 64 >> 9) << 4) + 15] = len;
var w = Array(80);
var a = 1732584193;
var b = -271733879;
var c = -1732584194;
var d = 271733878;
var e = -1009589776;
for(var i = 0; i < x.length; i += 16)
{
var olda = a;
var oldb = b;
var oldc = c;
var oldd = d;
var olde = e;
for(var j = 0; j < 80; j++)
{
if(j < 16)
w[j] = x[i + j];
else
w[j] = rol(w[j-3] ^ w[j-8] ^ w[j-14] ^ w[j-16], 1);
var t = safe_add(safe_add(rol(a, 5), sha1_ft(j, b, c, d)), safe_add(safe_add(e, w[j]), sha1_kt(j)));
e = d;
d = c;
c = rol(b, 30);
b = a;
a = t;
}
a = safe_add(a, olda);
b = safe_add(b, oldb);
c = safe_add(c, oldc);
d = safe_add(d, oldd);
e = safe_add(e, olde);
}
return Array(a, b, c, d, e);
}
if (raw == true)
{
return str_sha1(str1);
}
else
{
return hex_sha1(str1);
}
}
The standard library hashlib module includes sha1:
>>> import hashlib
>>> h = hashlib.sha1("lkjlkjlkj")
>>> h.digest()
"N]\x96\xb3:a^6\xa9A}.\x92\xea\xf6\xaa\x19'b{"
>>> h.hexdigest()
'4e5d96b33a615e36a9417d2e92eaf6aa1927627b'

Categories