|
2 | 2 | /// <reference path='../../../third_party/simple-rc4/simple-rc4.d.ts' /> |
3 | 3 | /// <reference path='../../../third_party/typings/browser.d.ts' /> |
4 | 4 |
|
| 5 | +import crypto = require('crypto'); |
5 | 6 | import logging = require('../logging/logging'); |
6 | 7 | import rc4 = require('simple-rc4'); |
7 | | -import sha1 = require('crypto/sha1'); |
8 | 8 | import transformer = require('./transformer'); |
9 | 9 |
|
10 | 10 | const log = new logging.Log('rc4 transformer'); |
@@ -69,18 +69,16 @@ export class Rc4Transformer implements transformer.Transformer { |
69 | 69 |
|
70 | 70 | // Applies RC4(IV[0,...,7] to bytes, as described in the pseudocode above. |
71 | 71 | private update_ = (r: Buffer, bytes:Buffer): void => { |
72 | | - // TODO: use the crypto module and avoid string conversion |
73 | | - const iv = sha1.str_sha1(Buffer.concat([this.key_, r]).toString('binary')); |
74 | | - const truncatedIv = new Buffer(iv, 'binary').slice(0, TRUNCATED_IV_LENGTH_BYTES); |
| 72 | + const hasher = crypto.createHash('sha1'); |
| 73 | + const iv = hasher.update(Buffer.concat([this.key_, r])); |
| 74 | + const truncatedIv = iv.digest().slice(0, TRUNCATED_IV_LENGTH_BYTES); |
75 | 75 | new rc4(truncatedIv).update(bytes); |
76 | 76 | } |
77 | 77 |
|
78 | 78 | public transform = (ab: ArrayBuffer): ArrayBuffer[] => { |
79 | 79 | const p = new Buffer(ab); |
80 | 80 |
|
81 | | - const r = new Buffer(R_LENGTH_BYTES); |
82 | | - crypto.getRandomValues(r); |
83 | | - |
| 81 | + const r = crypto.randomBytes(R_LENGTH_BYTES); |
84 | 82 | this.update_(r, p); |
85 | 83 |
|
86 | 84 | return [Buffer.concat([r, p]).buffer]; |
|
0 commit comments