123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112 |
- goog.provide('goog.crypt.Ctr');
- goog.require('goog.array');
- goog.require('goog.asserts');
- goog.require('goog.crypt');
- goog.crypt.Ctr = function(cipher) {
-
- this.cipher_ = cipher;
- };
- goog.crypt.Ctr.prototype.encrypt = function(plainText, initialVector) {
- goog.asserts.assert(
- initialVector.length == this.cipher_.BLOCK_SIZE,
- 'Initial vector must be size of one block.');
-
- var counter = goog.array.clone(initialVector);
- var keyStreamBlock = [];
- var encryptedArray = [];
- var plainTextBlock = [];
- while (encryptedArray.length < plainText.length) {
- keyStreamBlock = this.cipher_.encrypt(counter);
- goog.crypt.Ctr.incrementBigEndianCounter_(counter);
- plainTextBlock = goog.array.slice(
- plainText, encryptedArray.length,
- encryptedArray.length + this.cipher_.BLOCK_SIZE);
- goog.array.extend(
- encryptedArray,
- goog.crypt.xorByteArray(
- plainTextBlock,
- goog.array.slice(keyStreamBlock, 0, plainTextBlock.length)));
- }
- return encryptedArray;
- };
- goog.crypt.Ctr.prototype.decrypt = goog.crypt.Ctr.prototype.encrypt;
- goog.crypt.Ctr.incrementBigEndianCounter_ = function(counter) {
- for (var i = counter.length - 1; i >= 0; i--) {
- var currentByte = counter[i];
- currentByte = (currentByte + 1) & 0xFF;
- counter[i] = currentByte;
- if (currentByte != 0) {
-
-
- return;
- }
- }
- };
|