Skip to content

Commit bdcab8c

Browse files
committed
use crypto library, eliminating string conversions for hashing
1 parent 02dbd11 commit bdcab8c

1 file changed

Lines changed: 5 additions & 7 deletions

File tree

src/transformers/rc4.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
/// <reference path='../../../third_party/simple-rc4/simple-rc4.d.ts' />
33
/// <reference path='../../../third_party/typings/browser.d.ts' />
44

5+
import crypto = require('crypto');
56
import logging = require('../logging/logging');
67
import rc4 = require('simple-rc4');
7-
import sha1 = require('crypto/sha1');
88
import transformer = require('./transformer');
99

1010
const log = new logging.Log('rc4 transformer');
@@ -69,18 +69,16 @@ export class Rc4Transformer implements transformer.Transformer {
6969

7070
// Applies RC4(IV[0,...,7] to bytes, as described in the pseudocode above.
7171
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);
7575
new rc4(truncatedIv).update(bytes);
7676
}
7777

7878
public transform = (ab: ArrayBuffer): ArrayBuffer[] => {
7979
const p = new Buffer(ab);
8080

81-
const r = new Buffer(R_LENGTH_BYTES);
82-
crypto.getRandomValues(r);
83-
81+
const r = crypto.randomBytes(R_LENGTH_BYTES);
8482
this.update_(r, p);
8583

8684
return [Buffer.concat([r, p]).buffer];

0 commit comments

Comments
 (0)