arc4_test.js 1.9 KB

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