| 1 |
// Copyright 2010 The Closure Library Authors. All Rights Reserved. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS-IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
goog.provide('goog.crypt.Sha1Test'); |
| 16 |
goog.setTestOnly('goog.crypt.Sha1Test'); |
| 17 |
|
| 18 |
goog.require('goog.crypt'); |
| 19 |
goog.require('goog.crypt.Sha1'); |
| 20 |
goog.require('goog.crypt.hashTester'); |
| 21 |
goog.require('goog.testing.jsunit'); |
| 22 |
goog.require('goog.userAgent'); |
| 23 |
|
| 24 |
function testBasicOperations() { |
| 25 |
var sha1 = new goog.crypt.Sha1(); |
| 26 |
goog.crypt.hashTester.runBasicTests(sha1); |
| 27 |
} |
| 28 |
|
| 29 |
function testBlockOperations() { |
| 30 |
var sha1 = new goog.crypt.Sha1(); |
| 31 |
goog.crypt.hashTester.runBlockTests(sha1, 64); |
| 32 |
} |
| 33 |
|
| 34 |
function testHashing() { |
| 35 |
// Test vectors from: |
| 36 |
// csrc.nist.gov/publications/fips/fips180-2/fips180-2withchangenotice.pdf |
| 37 |
|
| 38 |
// Empty stream. |
| 39 |
var sha1 = new goog.crypt.Sha1(); |
| 40 |
assertEquals( |
| 41 |
'da39a3ee5e6b4b0d3255bfef95601890afd80709', |
| 42 |
goog.crypt.byteArrayToHex(sha1.digest())); |
| 43 |
|
| 44 |
// Test one-block message. |
| 45 |
sha1.reset(); |
| 46 |
sha1.update([0x61, 0x62, 0x63]); |
| 47 |
assertEquals( |
| 48 |
'a9993e364706816aba3e25717850c26c9cd0d89d', |
| 49 |
goog.crypt.byteArrayToHex(sha1.digest())); |
| 50 |
|
| 51 |
// Test multi-block message. |
| 52 |
sha1.reset(); |
| 53 |
sha1.update('abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq'); |
| 54 |
assertEquals( |
| 55 |
'84983e441c3bd26ebaae4aa1f95129e5e54670f1', |
| 56 |
goog.crypt.byteArrayToHex(sha1.digest())); |
| 57 |
|
| 58 |
// The following test might cause timeouts on IE7 and IE8. See b/22873770. |
| 59 |
if (!goog.userAgent.IE || goog.userAgent.isVersionOrHigher('9')) { |
| 60 |
// Test long message. |
| 61 |
var thousandAs = []; |
| 62 |
for (var i = 0; i < 1000; ++i) { |
| 63 |
thousandAs[i] = 0x61; |
| 64 |
} |
| 65 |
sha1.reset(); |
| 66 |
for (var i = 0; i < 1000; ++i) { |
| 67 |
sha1.update(thousandAs); |
| 68 |
} |
| 69 |
assertEquals( |
| 70 |
'34aa973cd4c4daa4f61eeb2bdbad27316534016f', |
| 71 |
goog.crypt.byteArrayToHex(sha1.digest())); |
| 72 |
} |
| 73 |
|
| 74 |
// Test standard message. |
| 75 |
sha1.reset(); |
| 76 |
sha1.update('The quick brown fox jumps over the lazy dog'); |
| 77 |
assertEquals( |
| 78 |
'2fd4e1c67a2d28fced849ee1bb76e7391b93eb12', |
| 79 |
goog.crypt.byteArrayToHex(sha1.digest())); |
| 80 |
} |