-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmd5.gs
65 lines (56 loc) · 1.75 KB
/
md5.gs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/**
* Usage1:
* `=MD5("YourStringToHash")`
* or
* `=MD5( A1 )` with the same string at A1 cell
* result:
* `FCE7453B7462D9DE0C56AFCCFB756193`.
* For your sure-ness you can verify it in your terminal as below.
* `$ md5 -s "YourStringToHash"`
* Usage2:
* `=MD5("YourStringToHash", true)` for short Hash
* result:
* `6MQH`
* Note that it has more conflict probability.
*
*
* @param {(string|Bytes[])} input The value to hash.
* @param {boolean} isShortMode Set true for 4 digit shortend hash, else returns usual MD5 hash.
* @return {string} The hashed input
* @customfunction
*
*/
function MD5( input, isShortMode )
{
var isShortMode = !!isShortMode; // Be sure to be bool
var txtHash = '';
var rawHash = Utilities.computeDigest(
Utilities.DigestAlgorithm.MD5,
input );
if ( ! isShortMode ) {
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 );
};
} else {
for ( j = 0; j < 16; j += 8 ) {
hashVal = ( rawHash[j] + rawHash[j+1] + rawHash[j+2] + rawHash[j+3] )
^ ( rawHash[j+4] + rawHash[j+5] + rawHash[j+6] + rawHash[j+7] );
if ( hashVal < 0 ) {
hashVal += 1024;
};
if ( hashVal.toString( 36 ).length == 1 ) {
txtHash += "0";
};
txtHash += hashVal.toString( 36 );
};
};
// change below to "txtHash.toUpperCase()" if needed
return txtHash;
}