103 lines
3.7 KiB
JavaScript
103 lines
3.7 KiB
JavaScript
// Copyright 2012 The Closure Library Authors. All Rights Reserved.
|
|
//
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
// you may not use this file except in compliance with the License.
|
|
// You may obtain a copy of the License at
|
|
//
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS-IS" BASIS,
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
|
|
goog.provide('goog.crypt.Sha256Test');
|
|
goog.setTestOnly('goog.crypt.Sha256Test');
|
|
|
|
goog.require('goog.array');
|
|
goog.require('goog.crypt');
|
|
goog.require('goog.crypt.Sha256');
|
|
goog.require('goog.crypt.hashTester');
|
|
goog.require('goog.testing.jsunit');
|
|
|
|
function testBasicOperations() {
|
|
var sha256 = new goog.crypt.Sha256();
|
|
goog.crypt.hashTester.runBasicTests(sha256);
|
|
}
|
|
|
|
function testHashing() {
|
|
// Some test vectors from:
|
|
// csrc.nist.gov/publications/fips/fips180-2/fips180-2withchangenotice.pdf
|
|
|
|
var sha256 = new goog.crypt.Sha256();
|
|
|
|
// Empty message.
|
|
sha256.update([]);
|
|
assertEquals(
|
|
'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855',
|
|
goog.crypt.byteArrayToHex(sha256.digest()));
|
|
|
|
// NIST one block test vector.
|
|
sha256.reset();
|
|
sha256.update(goog.crypt.stringToByteArray('abc'));
|
|
assertEquals(
|
|
'ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad',
|
|
goog.crypt.byteArrayToHex(sha256.digest()));
|
|
|
|
// NIST multi-block test vector.
|
|
sha256.reset();
|
|
sha256.update(
|
|
goog.crypt.stringToByteArray(
|
|
'abcdbcdecdefdefgefghfghighij' +
|
|
'hijkijkljklmklmnlmnomnopnopq'));
|
|
assertEquals(
|
|
'248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1',
|
|
goog.crypt.byteArrayToHex(sha256.digest()));
|
|
|
|
// Message larger than one block (but less than two).
|
|
sha256.reset();
|
|
var biggerThanOneBlock = 'abcdbcdecdefdefgefghfghighij' +
|
|
'hijkijkljklmklmnlmnomnopnopq' +
|
|
'asdfljhr78yasdfljh45opa78sdf' +
|
|
'120839414104897aavnasdfafasd';
|
|
assertTrue(biggerThanOneBlock.length > goog.crypt.Sha2.BLOCKSIZE_ &&
|
|
biggerThanOneBlock.length < 2 * goog.crypt.Sha2.BLOCKSIZE_);
|
|
sha256.update(goog.crypt.stringToByteArray(biggerThanOneBlock));
|
|
assertEquals(
|
|
'390a5035433e46b740600f3117d11ece3c64706dc889106666ac04fe4f458abc',
|
|
goog.crypt.byteArrayToHex(sha256.digest()));
|
|
|
|
// Message larger than two blocks.
|
|
sha256.reset();
|
|
var biggerThanTwoBlocks = 'abcdbcdecdefdefgefghfghighij' +
|
|
'hijkijkljklmklmnlmnomnopnopq' +
|
|
'asdfljhr78yasdfljh45opa78sdf' +
|
|
'120839414104897aavnasdfafasd' +
|
|
'laasdouvhalacbnalalseryalcla';
|
|
assertTrue(biggerThanTwoBlocks.length > 2 * goog.crypt.Sha2.BLOCKSIZE_);
|
|
sha256.update(goog.crypt.stringToByteArray(biggerThanTwoBlocks));
|
|
assertEquals(
|
|
'd655c513fd347e9be372d891f8bb42895ca310fabf6ead6681ebc66a04e84db5',
|
|
goog.crypt.byteArrayToHex(sha256.digest()));
|
|
}
|
|
|
|
function testPreschedule() {
|
|
// Test using prescheduled messages.
|
|
var sha = new goog.crypt.Sha256();
|
|
var message = goog.crypt.hexToByteArray(
|
|
'd9b28b643d16efc8a17a532c05deb79069421bf4cda67f58310ae3bc956e4720' +
|
|
'f9d2ab845d360fe8c19a734c25fed7b089623b14edc69f78512a03dcb58e6740');
|
|
|
|
var scheduled = sha.preschedule(message);
|
|
sha.scheduledUpdate(scheduled);
|
|
var w_scheduled = goog.array.toArray(sha.hash_);
|
|
var digest_scheduled = sha.digest();
|
|
|
|
sha.reset();
|
|
sha.update(message);
|
|
var w_afterupdate = goog.array.toArray(sha.hash_);
|
|
assertElementsEquals(w_scheduled, w_afterupdate);
|
|
assertElementsEquals(sha.digest(), digest_scheduled);
|
|
}
|