Skip to content

Commit 3e38f80

Browse files
committed
Merge pull request #398 from uProxy/trevj-node-crypto
replace crypto npm with calls to node crypto module
2 parents 5d0890b + 32b355c commit 3e38f80

4 files changed

Lines changed: 7 additions & 63 deletions

File tree

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
"browserify-istanbul": "^0.2.0",
3232
"browserify-zlib": "^0.1.4",
3333
"circular-json": "^0.3.0",
34-
"crypto": "^0.0.3",
3534
"es6-promise": "^3.0.2",
3635
"forge-min": "^0.6.20",
3736
"freedom": "^0.6.25",

src/cloud/social/provider.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
11
/// <reference path='../../../../third_party/typings/browser.d.ts' />
2-
/// <reference path='../../../../third_party/sha1/sha1.d.ts' />
32

43
require('../social/monkey/process');
54

65
import arraybuffers = require('../../arraybuffers/arraybuffers');
6+
import crypto = require('crypto');
77
import linefeeder = require('../../net/linefeeder');
88
import logging = require('../../logging/logging');
99
import promises = require('../../promises/promises');
1010
import queue = require('../../handler/queue');
1111

12-
import sha1 = require('crypto/sha1');
13-
1412
// https://github.com/borisyankov/DefinitelyTyped/blob/master/ssh2/ssh2-tests.ts
1513
import * as ssh2 from 'ssh2';
1614
var Client = require('ssh2').Client;
@@ -483,7 +481,7 @@ class Connection {
483481
if (this.invite_.hostKey) {
484482
connectConfig.hostHash = 'sha1';
485483
let keyBuffer = new Buffer(this.invite_.hostKey, 'base64');
486-
let expectedHash = sha1.hex_sha1(keyBuffer.toString('binary'));
484+
let expectedHash = crypto.createHash('sha1').update(keyBuffer).digest('hex');
487485
connectConfig.hostVerifier = (keyHash :string) => {
488486
return keyHash === expectedHash;
489487
};

src/turn/messages.ts

Lines changed: 5 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
1-
/// <reference path='../../../third_party/sha1/sha1.d.ts' />
1+
/// <reference path='../../../third_party/typings/browser.d.ts' />
22

33
import arraybuffers = require('../arraybuffers/arraybuffers');
4-
4+
import crypto = require('crypto');
55
import net = require('../net/net.types');
66

7-
import sha1 = require('crypto/sha1');
8-
97
/**
108
* Utilities for decoding and encoding STUN messages.
119
* TURN uses STUN messages, adding some methods and attributes.
@@ -125,9 +123,7 @@ export var REALM = 'myrealm';
125123
* supply our crypto library with a UTF-8 string generated from the 16 bytes.
126124
*/
127125
// TODO: dynamic username/password would be pretty easy to implement
128-
// TODO: would be nice to run uint8array -> string on boot but the files
129-
// don't seem to load in the right order...might be a typescript bug
130-
var HMAC_KEY = new Uint8Array([
126+
const HMAC_KEY = new Buffer([
131127
0xbd, 0x8a, 0xdb, 0x31,
132128
0x7d, 0x5d, 0x54, 0x2e,
133129
0x5e, 0x2a, 0xba, 0x5b,
@@ -328,25 +324,10 @@ export function formatStunMessageWithIntegrity(message:StunMessage) : Uint8Array
328324
* supplied byte array.
329325
*/
330326
export function computeHash(bytes:Uint8Array) : Uint8Array {
331-
var keyAsString = arraybuffers.arrayBufferToString(HMAC_KEY.buffer);
332327
// MESSAGE-INTEGRITY attributes are always 24 bytes long:
333328
// 4 bytes header + 20 bytes hash
334-
var bytesToBeHashed = bytes.subarray(0, bytes.byteLength - 24);
335-
// Think of the next few lines as uint8ArrayToString().
336-
// This is necessary because, depending on how b is constructed,
337-
// b.buffer is not guaranteed to equal a, where b is a Uint8Array
338-
// view on an ArrayBuffer a (in particular, views created with
339-
// subarray will share the same parent ArrayBuffer).
340-
// TODO: add uint8ArrayToString to uproxy-build-tools
341-
var a :string[] = [];
342-
for (var i = 0; i < bytesToBeHashed.length; ++i) {
343-
a.push(String.fromCharCode(bytes[i]));
344-
}
345-
var bytesToBeHashedAsString = a.join('');
346-
347-
var hashAsString = sha1.str_hmac_sha1(keyAsString,
348-
bytesToBeHashedAsString);
349-
return new Uint8Array(arraybuffers.stringToArrayBuffer(hashAsString));
329+
const bytesToBeHashed = bytes.subarray(0, bytes.byteLength - 24);
330+
return crypto.createHmac('sha1', HMAC_KEY).update(bytesToBeHashed).digest();
350331
}
351332

352333
/**

third_party/sha1/sha1.d.ts

Lines changed: 0 additions & 34 deletions
This file was deleted.

0 commit comments

Comments
 (0)