1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
9 #include "base/basictypes.h"
13 // Implementation of SHA-1. Only handles data in byte-sized blocks,
14 // which simplifies the code a fair bit.
16 // Identifier names follow notation in FIPS PUB 180-3, where you'll
17 // also find a description of the algorithm:
18 // http://csrc.nist.gov/publications/fips/fips180-3/fips180-3_final.pdf
22 // SecureHashAlgorithm sha;
23 // while(there is data to hash)
24 // sha.Update(moredata, size of data);
26 // memcpy(somewhere, sha.Digest(), 20);
28 // to reuse the instance of sha, call sha.Init();
30 // TODO(jhawkins): Replace this implementation with a per-platform
31 // implementation using each platform's crypto library. See
32 // http://crbug.com/47218
34 class SecureHashAlgorithm
{
36 SecureHashAlgorithm() { Init(); }
38 static const int kDigestSizeBytes
;
41 void Update(const void* data
, size_t nbytes
);
44 // 20 bytes of message digest.
45 const unsigned char* Digest() const {
46 return reinterpret_cast<const unsigned char*>(H
);
66 static inline uint32
f(uint32 t
, uint32 B
, uint32 C
, uint32 D
) {
68 return (B
& C
) | ((~B
) & D
);
72 return (B
& C
) | (B
& D
) | (C
& D
);
78 static inline uint32
S(uint32 n
, uint32 X
) {
79 return (X
<< n
) | (X
>> (32-n
));
82 static inline uint32
K(uint32 t
) {
94 static inline void swapends(uint32
* t
) {
95 *t
= (*t
>> 24) | ((*t
>> 8) & 0xff00) | ((*t
& 0xff00) << 8) | (*t
<< 24);
98 const int SecureHashAlgorithm::kDigestSizeBytes
= 20;
100 void SecureHashAlgorithm::Init() {
115 void SecureHashAlgorithm::Final() {
119 for (int t
= 0; t
< 5; ++t
)
123 void SecureHashAlgorithm::Update(const void* data
, size_t nbytes
) {
124 const uint8
* d
= reinterpret_cast<const uint8
*>(data
);
133 void SecureHashAlgorithm::Pad() {
137 // pad out to next block
144 while (cursor
< 64-8)
147 M
[cursor
++] = (l
>> 56) & 0xff;
148 M
[cursor
++] = (l
>> 48) & 0xff;
149 M
[cursor
++] = (l
>> 40) & 0xff;
150 M
[cursor
++] = (l
>> 32) & 0xff;
151 M
[cursor
++] = (l
>> 24) & 0xff;
152 M
[cursor
++] = (l
>> 16) & 0xff;
153 M
[cursor
++] = (l
>> 8) & 0xff;
154 M
[cursor
++] = l
& 0xff;
157 void SecureHashAlgorithm::Process() {
160 // Each a...e corresponds to a section in the FIPS 180-3 algorithm.
164 // W and M are in a union, so no need to memcpy.
165 // memcpy(W, M, sizeof(M));
166 for (t
= 0; t
< 16; ++t
)
170 for (t
= 16; t
< 80; ++t
)
171 W
[t
] = S(1, W
[t
- 3] ^ W
[t
- 8] ^ W
[t
- 14] ^ W
[t
- 16]);
181 for (t
= 0; t
< 80; ++t
) {
182 uint32 TEMP
= S(5, A
) + f(t
, B
, C
, D
) + E
+ W
[t
] + K(t
);
200 std::string
SHA1HashString(const std::string
& str
) {
201 char hash
[SecureHashAlgorithm::kDigestSizeBytes
];
202 SHA1HashBytes(reinterpret_cast<const unsigned char*>(str
.c_str()),
203 str
.length(), reinterpret_cast<unsigned char*>(hash
));
204 return std::string(hash
, SecureHashAlgorithm::kDigestSizeBytes
);
207 void SHA1HashBytes(const unsigned char* data
, size_t len
,
208 unsigned char* hash
) {
209 SecureHashAlgorithm sha
;
210 sha
.Update(data
, len
);
213 memcpy(hash
, sha
.Digest(), SecureHashAlgorithm::kDigestSizeBytes
);