forked from TheAlgorithms/JavaScript
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSHA1.js
More file actions
Latest commit
176 lines (157 loc) · 4.41 KB
/
Copy pathSHA1.js
File metadata and controls
176 lines (157 loc) · 4.41 KB
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
//= ===============================================================
// SHA1.js
//
// Module that replicates the SHA-1 Cryptographic Hash
// function in Javascript.
//= ===============================================================
// main variables
constCHAR_SIZE=8
/**
* Adds padding to binary/hex string representation
*
* @param {string} str - string representation (binary/hex)
* @param {int} bits - total number of bits wanted
* @return {string} - string representation padding with empty (0) bits
*
* @example
* pad("10011", 8); // "00010011"
*/
functionpad(str,bits){
letres=str
while(res.length%bits!==0){
res='0'+res
}
returnres
}
/**
* Separates string into chunks of the same size
*
* @param {string} str - string to separate into chunks
* @param {int} size - number of characters wanted in each chunk
* @return {array} - array of original string split into chunks
*
* @example
* chunkify("this is a test", 2)
*/
functionchunkify(str,size){
constchunks=[]
for(leti=0;i<str.length;i+=size){
chunks.push(str.slice(i,i+size))
}
returnchunks
}
/**
* Rotates string representation of bits to the left
*
* @param {string} bits - string representation of bits
* @param {int} turns - number of rotations to make
* @return {string} - string representation of bits after rotation
*
* @example
* rotateLeft("1011", 3); // "1101"
*/
functionrotateLeft(bits,turns){
returnbits.substr(turns)+bits.substr(0,turns)
}
/**
* Pre-processes message to feed the algorithm loop
*
* @param {string} message - message to pre-process
* @return {string} - processed message
*/
functionpreProcess(message){
// convert message to binary representation padded to
// 8 bits, and add 1
letm=
message
.split('')
.map((e)=>e.charCodeAt(0))
.map((e)=>e.toString(2))
.map((e)=>pad(e,8))
.join('')+'1'
// extend message by adding empty bits (0)
while(m.length%512!==448){
m+='0'
}
// length of message in binary, padded, and extended
// to a 64 bit representation
letml=(message.length*CHAR_SIZE).toString(2)
ml=pad(ml,8)
ml='0'.repeat(64-ml.length)+ml
returnm+ml
}
/**
* Hashes message using SHA-1 Cryptographic Hash Function
*
* @param {string} message - message to hash
* @return {string} - message digest (hash value)
*/
functionSHA1(message){
// main variables
letH0=0x67452301
letH1=0xefcdab89
letH2=0x98badcfe
letH3=0x10325476
letH4=0xc3d2e1f0
// pre-process message and split into 512 bit chunks
constbits=preProcess(message)
constchunks=chunkify(bits,512)
chunks.forEach(function(chunk,i){
// break each chunk into 16 32-bit words
constwords=chunkify(chunk,32)
// extend 16 32-bit words to 80 32-bit words
for(leti=16;i<80;i++){
constval=[words[i-3],words[i-8],words[i-14],words[i-16]]
.map((e)=>parseInt(e,2))
.reduce((acc,curr)=>curr^acc,0)
constbin=(val>>>0).toString(2)
constpaddedBin=pad(bin,32)
constword=rotateLeft(paddedBin,1)
words.push(word)
}
// initialize variables for this chunk
let[a,b,c,d,e]=[H0,H1,H2,H3,H4]
for(leti=0;i<80;i++){
letf,k
if(i<20){
f=(b&c)|(~b&d)
k=0x5a827999
}elseif(i<40){
f=b^c^d
k=0x6ed9eba1
}elseif(i<60){
f=(b&c)|(b&d)|(c&d)
k=0x8f1bbcdc
}else{
f=b^c^d
k=0xca62c1d6
}
// make sure f is unsigned
f>>>=0
constaRot=rotateLeft(pad(a.toString(2),32),5)
constaInt=parseInt(aRot,2)>>>0
constwordInt=parseInt(words[i],2)>>>0
constt=aInt+f+e+k+wordInt
e=d>>>0
d=c>>>0
constbRot=rotateLeft(pad(b.toString(2),32),30)
c=parseInt(bRot,2)>>>0
b=a>>>0
a=t>>>0
}
// add values for this chunk to main hash variables (unsigned)
H0=(H0+a)>>>0
H1=(H1+b)>>>0
H2=(H2+c)>>>0
H3=(H3+d)>>>0
H4=(H4+e)>>>0
})
// combine hash values of main hash variables and return
constHH=[H0,H1,H2,H3,H4]
.map((e)=>e.toString(16))
.map((e)=>pad(e,8))
.join('')
returnHH
}
// export SHA1 function
export{SHA1}