aboutsummaryrefslogtreecommitdiff
path: root/contexts/data/lib/closure-library/closure/goog/crypt/sha1_test.html
blob: beffc551974ffc27274c5049101be0284b74eb52 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
<!DOCTYPE html>
<html>
<!--
Copyright 2010 The Closure Library Authors. All Rights Reserved.

Use of this source code is governed by the Apache License, Version 2.0.
See the COPYING file for details.
-->
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Closure Unit Tests - goog.crypt.sha1</title>
<script src="../base.js"></script>
<script>
  goog.require('goog.crypt');
  goog.require('goog.crypt.hash_test');
  goog.require('goog.crypt.Sha1');
  goog.require('goog.testing.jsunit');
  goog.require('goog.userAgent');
</script>
</head>
<body>
<script>

/**
 * Helper function to convert a byte array to a hex string
 */

function testBasicOperations() {
  var sha1 = new goog.crypt.Sha1();
  goog.crypt.hash_test.runBasicTests(sha1);
}

function testHashing() {
  // Empty stream.
  var sha1 = new goog.crypt.Sha1();
  assertEquals('da39a3ee5e6b4b0d3255bfef95601890afd80709',
               goog.crypt.byteArrayToHex(sha1.digest()));

  // Empty stream with an empty update.
  sha1.reset();
  sha1.update([]);
  assertEquals('da39a3ee5e6b4b0d3255bfef95601890afd80709',
               goog.crypt.byteArrayToHex(sha1.digest()));

  // Test one-block message.
  sha1.reset();
  sha1.update([0x61, 0x62, 0x63]);
  assertEquals('a9993e364706816aba3e25717850c26c9cd0d89d',
               goog.crypt.byteArrayToHex(sha1.digest()));

  // Test multi-block message.
  sha1.reset();
  sha1.update('abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq');
  assertEquals('84983e441c3bd26ebaae4aa1f95129e5e54670f1',
               goog.crypt.byteArrayToHex(sha1.digest()));

  // The following test might cause timeouts on IE7.
  if (!goog.userAgent.IE || goog.userAgent.isVersion('8')) {
    // Test long message.
    var thousandAs = [];
    for (var i = 0; i < 1000; ++i) {
      thousandAs[i] = 0x61;
    }
    sha1.reset();
    for (var i = 0; i < 1000; ++i) {
      sha1.update(thousandAs);
    }
    assertEquals('34aa973cd4c4daa4f61eeb2bdbad27316534016f',
                 goog.crypt.byteArrayToHex(sha1.digest()));
  }

  // Test standard message.
  sha1.reset();
  sha1.update('The quick brown fox jumps over the lazy dog');
  assertEquals('2fd4e1c67a2d28fced849ee1bb76e7391b93eb12',
               goog.crypt.byteArrayToHex(sha1.digest()));
}

</script>
</body>
</html>