Debian Codegraph @

closure-library/closure/goog/crypt/sha224_test.js

raw
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.Sha224Test');
16 goog.setTestOnly('goog.crypt.Sha224Test');
17
18 goog.require('goog.crypt');
19 goog.require('goog.crypt.Sha224');
20 goog.require('goog.crypt.hashTester');
21 goog.require('goog.testing.jsunit');
22
23 function testBasicOperations() {
24 var sha224 = new goog.crypt.Sha224();
25 goog.crypt.hashTester.runBasicTests(sha224);
26 }
27
28 function testHashing() {
29 // Some test vectors from:
30 // csrc.nist.gov/publications/fips/fips180-2/fips180-2withchangenotice.pdf
31
32 var sha224 = new goog.crypt.Sha224();
33
34 // NIST one block test vector.
35 sha224.update(goog.crypt.stringToByteArray('abc'));
36 assertEquals(
37 '23097d223405d8228642a477bda255b32aadbce4bda0b3f7e36c9da7',
38 goog.crypt.byteArrayToHex(sha224.digest()));
39
40 // NIST multi-block test vector.
41 sha224.reset();
42 sha224.update(
43 goog.crypt.stringToByteArray(
44 'abcdbcdecdefdefgefghfghighij' +
45 'hijkijkljklmklmnlmnomnopnopq'));
46 assertEquals(
47 '75388b16512776cc5dba5da1fd890150b0c6455cb4f58b1952522525',
48 goog.crypt.byteArrayToHex(sha224.digest()));
49
50 // Message larger than one block (but less than two).
51 sha224.reset();
52 var biggerThanOneBlock = 'abcdbcdecdefdefgefghfghighij' +
53 'hijkijkljklmklmnlmnomnopnopq' +
54 'asdfljhr78yasdfljh45opa78sdf' +
55 '120839414104897aavnasdfafasd';
56 assertTrue(
57 biggerThanOneBlock.length > goog.crypt.Sha2.BLOCKSIZE_ &&
58 biggerThanOneBlock.length < 2 * goog.crypt.Sha2.BLOCKSIZE_);
59 sha224.update(goog.crypt.stringToByteArray(biggerThanOneBlock));
60 assertEquals(
61 '27c9b678012becd6891bac653f355b2d26f63132e840644d565f5dac',
62 goog.crypt.byteArrayToHex(sha224.digest()));
63
64 // Message larger than two blocks.
65 sha224.reset();
66 var biggerThanTwoBlocks = 'abcdbcdecdefdefgefghfghighij' +
67 'hijkijkljklmklmnlmnomnopnopq' +
68 'asdfljhr78yasdfljh45opa78sdf' +
69 '120839414104897aavnasdfafasd' +
70 'laasdouvhalacbnalalseryalcla';
71 assertTrue(biggerThanTwoBlocks.length > 2 * goog.crypt.Sha2.BLOCKSIZE_);
72 sha224.update(goog.crypt.stringToByteArray(biggerThanTwoBlocks));
73 assertEquals(
74 '1c2c1455cc984eef6f25ec9d79b1c661b3794887c3d0b24111ed9803',
75 goog.crypt.byteArrayToHex(sha224.digest()));
76 }