| 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.Arc4Test'); |
| 16 |
goog.setTestOnly('goog.crypt.Arc4Test'); |
| 17 |
|
| 18 |
goog.require('goog.array'); |
| 19 |
goog.require('goog.crypt.Arc4'); |
| 20 |
goog.require('goog.testing.jsunit'); |
| 21 |
|
| 22 |
function testEncryptionDecryption() { |
| 23 |
var key = [0x25, 0x26, 0x27, 0x28]; |
| 24 |
var startArray = [0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67]; |
| 25 |
var byteArray = [0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67]; |
| 26 |
|
| 27 |
var arc4 = new goog.crypt.Arc4(); |
| 28 |
arc4.setKey(key); |
| 29 |
arc4.crypt(byteArray); |
| 30 |
|
| 31 |
assertArrayEquals(byteArray, [0x51, 0xBB, 0xDD, 0x95, 0x9B, 0x42, 0x34]); |
| 32 |
|
| 33 |
// The same key and crypt call should unencrypt the data back to its original |
| 34 |
// state |
| 35 |
arc4 = new goog.crypt.Arc4(); |
| 36 |
arc4.setKey(key); |
| 37 |
arc4.crypt(byteArray); |
| 38 |
assertArrayEquals(byteArray, startArray); |
| 39 |
} |
| 40 |
|
| 41 |
function testDiscard() { |
| 42 |
var key = [0x25, 0x26, 0x27, 0x28]; |
| 43 |
var data = [0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67]; |
| 44 |
|
| 45 |
var arc4 = new goog.crypt.Arc4(); |
| 46 |
arc4.setKey(key); |
| 47 |
arc4.discard(256); |
| 48 |
var withDiscard = goog.array.clone(data); |
| 49 |
arc4.crypt(withDiscard); |
| 50 |
|
| 51 |
// First encrypting a dummy array should give the same result as |
| 52 |
// discarding. |
| 53 |
arc4 = new goog.crypt.Arc4(); |
| 54 |
arc4.setKey(key); |
| 55 |
var withCrypt = goog.array.clone(data); |
| 56 |
arc4.crypt(new Array(256)); |
| 57 |
arc4.crypt(withCrypt); |
| 58 |
assertArrayEquals(withDiscard, withCrypt); |
| 59 |
} |