Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(192)

Side by Side Diff: src/utils/SkSHA1.cpp

Issue 14265010: Make SkSHA1 and SkM5 use common SkDigestHash result type (Closed) Base URL: http://skia.googlecode.com/svn/trunk/
Patch Set: switch_to_MD5 Created 7 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 /* 1 /*
2 * Copyright 2013 Google Inc. 2 * Copyright 2013 Google Inc.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license that can be 4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file. 5 * found in the LICENSE file.
6 * 6 *
7 * The following code is based on the description in RFC 3174. 7 * The following code is based on the description in RFC 3174.
8 * http://www.ietf.org/rfc/rfc3174.txt 8 * http://www.ietf.org/rfc/rfc3174.txt
9 */ 9 */
10 10
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
51 bufferIndex = 0; 51 bufferIndex = 0;
52 } else { 52 } else {
53 inputIndex = 0; 53 inputIndex = 0;
54 } 54 }
55 55
56 memcpy(&this->buffer[bufferIndex], &input[inputIndex], inputLength - inputIn dex); 56 memcpy(&this->buffer[bufferIndex], &input[inputIndex], inputLength - inputIn dex);
57 57
58 this->byteCount += inputLength; 58 this->byteCount += inputLength;
59 } 59 }
60 60
61 void SkSHA1::finish(Digest& digest) { 61 void SkSHA1::finish(SkHashDigest& digest) {
62 // Get the number of bits before padding. 62 // Get the number of bits before padding.
63 uint8_t bits[8]; 63 uint8_t bits[8];
64 encode(bits, this->byteCount << 3); 64 encode(bits, this->byteCount << 3);
65 65
66 // Pad out to 56 mod 64. 66 // Pad out to 56 mod 64.
67 unsigned int bufferIndex = (unsigned int)(this->byteCount & 0x3F); 67 unsigned int bufferIndex = (unsigned int)(this->byteCount & 0x3F);
68 unsigned int paddingLength = (bufferIndex < 56) ? (56 - bufferIndex) : (120 - bufferIndex); 68 unsigned int paddingLength = (bufferIndex < 56) ? (56 - bufferIndex) : (120 - bufferIndex);
69 static uint8_t PADDING[64] = { 69 static uint8_t PADDING[64] = {
70 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 70 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
71 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 71 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
72 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 72 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
73 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 73 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
74 }; 74 };
75 this->update(PADDING, paddingLength); 75 this->update(PADDING, paddingLength);
76 76
77 // Append length (length before padding, will cause final update). 77 // Append length (length before padding, will cause final update).
78 this->update(bits, 8); 78 this->update(bits, 8);
79 79
80 // Write out digest. 80 // Write out digest.
81 encode(digest.data, this->state); 81 // TODO(epoger): eliminate the extra 20-byte copy, by writing directly into digest.data???
82 // NO, we can't do that, because digest.data is immutable (wrapped in SkData ).
83 // Or:
84 // 1. malloc 20 bytes here (wrapped in an SkData object),
85 // 2. write the digest into the SkData-wrapped 20 bytes,
86 // 3. bump the ref counter on that SkData-wrapped 20 bytes,
87 // 4. replace digest's fSkDataPtr with the new one
88 size_t digestSize = 20;
89 uint8_t tempDigestData[digestSize];
90 encode(tempDigestData, this->state);
91 digest.copyFrom(tempDigestData, digestSize);
82 92
83 #if defined(SK_SHA1_CLEAR_DATA) 93 #if defined(SK_SHA1_CLEAR_DATA)
84 // Clear state. 94 // Clear state.
85 memset(this, 0, sizeof(*this)); 95 memset(this, 0, sizeof(*this));
86 #endif 96 #endif
87 } 97 }
88 98
89 struct F1 { uint32_t operator()(uint32_t B, uint32_t C, uint32_t D) { 99 struct F1 { uint32_t operator()(uint32_t B, uint32_t C, uint32_t D) {
90 return (B & C) | ((~B) & D); 100 return (B & C) | ((~B) & D);
91 //return D ^ (B & (C ^ D)); 101 //return D ^ (B & (C ^ D));
(...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after
259 static void encode(uint8_t output[8], const uint64_t input) { 269 static void encode(uint8_t output[8], const uint64_t input) {
260 output[0] = (uint8_t)((input >> 56) & 0xff); 270 output[0] = (uint8_t)((input >> 56) & 0xff);
261 output[1] = (uint8_t)((input >> 48) & 0xff); 271 output[1] = (uint8_t)((input >> 48) & 0xff);
262 output[2] = (uint8_t)((input >> 40) & 0xff); 272 output[2] = (uint8_t)((input >> 40) & 0xff);
263 output[3] = (uint8_t)((input >> 32) & 0xff); 273 output[3] = (uint8_t)((input >> 32) & 0xff);
264 output[4] = (uint8_t)((input >> 24) & 0xff); 274 output[4] = (uint8_t)((input >> 24) & 0xff);
265 output[5] = (uint8_t)((input >> 16) & 0xff); 275 output[5] = (uint8_t)((input >> 16) & 0xff);
266 output[6] = (uint8_t)((input >> 8) & 0xff); 276 output[6] = (uint8_t)((input >> 8) & 0xff);
267 output[7] = (uint8_t)((input ) & 0xff); 277 output[7] = (uint8_t)((input ) & 0xff);
268 } 278 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698