MCPcopy Create free account
hub / github.com/TheAlgorithms/Go / Hash

Function Hash

hashing/sha1/sha1.go:43–110  ·  view source on GitHub ↗

Hash computes the SHA-1 hash of the input message

(message []byte)

Source from the content-addressed store, hash-verified

41
42// Hash computes the SHA-1 hash of the input message
43func Hash(message []byte) [20]byte {
44 message = pad(message)
45
46 // Initialize variables
47 a, b, c, d, e := h0, h1, h2, h3, h4
48
49 // Process the message in successive 512-bit chunks
50 for i := 0; i < len(message); i += 64 {
51 var w [80]uint32
52 chunk := message[i : i+64]
53
54 // Break chunk into sixteen 32-bit big-endian words
55 for j := 0; j < 16; j++ {
56 w[j] = binary.BigEndian.Uint32(chunk[j*4 : (j+1)*4])
57 }
58
59 // Extend the sixteen 32-bit words into eighty 32-bit words
60 for j := 16; j < 80; j++ {
61 w[j] = leftRotate(w[j-3]^w[j-8]^w[j-14]^w[j-16], 1)
62 }
63
64 // Initialize hash value for this chunk
65 A, B, C, D, E := a, b, c, d, e
66
67 // Main loop
68 for j := 0; j < 80; j++ {
69 var f, k uint32
70 switch {
71 case j < 20:
72 f = (B & C) | ((^B) & D)
73 k = 0x5A827999
74 case j < 40:
75 f = B ^ C ^ D
76 k = 0x6ED9EBA1
77 case j < 60:
78 f = (B & C) | (B & D) | (C & D)
79 k = 0x8F1BBCDC
80 default:
81 f = B ^ C ^ D
82 k = 0xCA62C1D6
83 }
84
85 temp := leftRotate(A, 5) + f + E + k + w[j]
86 E = D
87 D = C
88 C = leftRotate(B, 30)
89 B = A
90 A = temp
91 }
92
93 // Add this chunk's hash to result so far
94 a += A
95 b += B
96 c += C
97 d += D
98 e += E
99 }
100

Callers 1

TestHashFunction · 0.70

Calls 2

padFunction · 0.70
leftRotateFunction · 0.70

Tested by 1

TestHashFunction · 0.56