| 1 |
// Copyright 2012 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.Sha256Test'); |
| 16 |
goog.setTestOnly('goog.crypt.Sha256Test'); |
| 17 |
|
| 18 |
goog.require('goog.crypt'); |
| 19 |
goog.require('goog.crypt.Sha256'); |
| 20 |
goog.require('goog.crypt.hashTester'); |
| 21 |
goog.require('goog.testing.jsunit'); |
| 22 |
|
| 23 |
function testBasicOperations() { |
| 24 |
var sha256 = new goog.crypt.Sha256(); |
| 25 |
goog.crypt.hashTester.runBasicTests(sha256); |
| 26 |
} |
| 27 |
|
| 28 |
function testHashing() { |
| 29 |
// Some test vectors from: |
| 30 |
// csrc.nist.gov/publications/fips/fips180-2/fips180-2withchangenotice.pdf |
| 31 |
|
| 32 |
var sha256 = new goog.crypt.Sha256(); |
| 33 |
|
| 34 |
// Empty message. |
| 35 |
sha256.update([]); |
| 36 |
assertEquals( |
| 37 |
'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855', |
| 38 |
goog.crypt.byteArrayToHex(sha256.digest())); |
| 39 |
|
| 40 |
// NIST one block test vector. |
| 41 |
sha256.reset(); |
| 42 |
sha256.update(goog.crypt.stringToByteArray('abc')); |
| 43 |
assertEquals( |
| 44 |
'ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad', |
| 45 |
goog.crypt.byteArrayToHex(sha256.digest())); |
| 46 |
|
| 47 |
// NIST multi-block test vector. |
| 48 |
sha256.reset(); |
| 49 |
sha256.update( |
| 50 |
goog.crypt.stringToByteArray( |
| 51 |
'abcdbcdecdefdefgefghfghighij' + |
| 52 |
'hijkijkljklmklmnlmnomnopnopq')); |
| 53 |
assertEquals( |
| 54 |
'248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1', |
| 55 |
goog.crypt.byteArrayToHex(sha256.digest())); |
| 56 |
|
| 57 |
// Message larger than one block (but less than two). |
| 58 |
sha256.reset(); |
| 59 |
var biggerThanOneBlock = 'abcdbcdecdefdefgefghfghighij' + |
| 60 |
'hijkijkljklmklmnlmnomnopnopq' + |
| 61 |
'asdfljhr78yasdfljh45opa78sdf' + |
| 62 |
'120839414104897aavnasdfafasd'; |
| 63 |
assertTrue( |
| 64 |
biggerThanOneBlock.length > goog.crypt.Sha2.BLOCKSIZE_ && |
| 65 |
biggerThanOneBlock.length < 2 * goog.crypt.Sha2.BLOCKSIZE_); |
| 66 |
sha256.update(goog.crypt.stringToByteArray(biggerThanOneBlock)); |
| 67 |
assertEquals( |
| 68 |
'390a5035433e46b740600f3117d11ece3c64706dc889106666ac04fe4f458abc', |
| 69 |
goog.crypt.byteArrayToHex(sha256.digest())); |
| 70 |
|
| 71 |
// Message larger than two blocks. |
| 72 |
sha256.reset(); |
| 73 |
var biggerThanTwoBlocks = 'abcdbcdecdefdefgefghfghighij' + |
| 74 |
'hijkijkljklmklmnlmnomnopnopq' + |
| 75 |
'asdfljhr78yasdfljh45opa78sdf' + |
| 76 |
'120839414104897aavnasdfafasd' + |
| 77 |
'laasdouvhalacbnalalseryalcla'; |
| 78 |
assertTrue(biggerThanTwoBlocks.length > 2 * goog.crypt.Sha2.BLOCKSIZE_); |
| 79 |
sha256.update(goog.crypt.stringToByteArray(biggerThanTwoBlocks)); |
| 80 |
assertEquals( |
| 81 |
'd655c513fd347e9be372d891f8bb42895ca310fabf6ead6681ebc66a04e84db5', |
| 82 |
goog.crypt.byteArrayToHex(sha256.digest())); |
| 83 |
} |
| 84 |
|
| 85 |
|
| 86 |
/** Check that the code checks for bad input */ |
| 87 |
function testBadInput() { |
| 88 |
assertThrows('Bad input', function() { new goog.crypt.Sha256().update({}); }); |
| 89 |
assertThrows('Floating point not allows', function() { |
| 90 |
new goog.crypt.Sha256().update([1, 2, 3, 4, 4.5]); |
| 91 |
}); |
| 92 |
assertThrows('Negative not allowed', function() { |
| 93 |
new goog.crypt.Sha256().update([1, 2, 3, 4, -10]); |
| 94 |
}); |
| 95 |
assertThrows('Must be byte array', function() { |
| 96 |
new goog.crypt.Sha256().update([1, 2, 3, 4, {}]); |
| 97 |
}); |
| 98 |
} |