|
1 | | -/// <reference path='../../../third_party/sha1/sha1.d.ts' /> |
| 1 | +/// <reference path='../../../third_party/typings/browser.d.ts' /> |
2 | 2 |
|
3 | 3 | import arraybuffers = require('../arraybuffers/arraybuffers'); |
4 | | - |
| 4 | +import crypto = require('crypto'); |
5 | 5 | import net = require('../net/net.types'); |
6 | 6 |
|
7 | | -import sha1 = require('crypto/sha1'); |
8 | | - |
9 | 7 | /** |
10 | 8 | * Utilities for decoding and encoding STUN messages. |
11 | 9 | * TURN uses STUN messages, adding some methods and attributes. |
@@ -125,9 +123,7 @@ export var REALM = 'myrealm'; |
125 | 123 | * supply our crypto library with a UTF-8 string generated from the 16 bytes. |
126 | 124 | */ |
127 | 125 | // 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([ |
131 | 127 | 0xbd, 0x8a, 0xdb, 0x31, |
132 | 128 | 0x7d, 0x5d, 0x54, 0x2e, |
133 | 129 | 0x5e, 0x2a, 0xba, 0x5b, |
@@ -328,25 +324,10 @@ export function formatStunMessageWithIntegrity(message:StunMessage) : Uint8Array |
328 | 324 | * supplied byte array. |
329 | 325 | */ |
330 | 326 | export function computeHash(bytes:Uint8Array) : Uint8Array { |
331 | | - var keyAsString = arraybuffers.arrayBufferToString(HMAC_KEY.buffer); |
332 | 327 | // MESSAGE-INTEGRITY attributes are always 24 bytes long: |
333 | 328 | // 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(); |
350 | 331 | } |
351 | 332 |
|
352 | 333 | /** |
|
0 commit comments