Skip to content

Commit 3210c77

Browse files
committed
Merge pull request #400 from uProxy/trevj-djb2-move
de-factor djb2 to peerconnection
2 parents 3e38f80 + f65a820 commit 3210c77

2 files changed

Lines changed: 12 additions & 12 deletions

File tree

src/crypto/djb2hash.ts

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

src/webrtc/peerconnection.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
/// <reference path='../../../third_party/typings/browser.d.ts' />
22

33
import datachannel = require('./datachannel');
4-
import djb2 = require('../crypto/djb2hash');
54
import handler = require('../handler/queue');
65
import logging = require('../logging/logging');
76
import signals = require('./signals');
@@ -23,6 +22,16 @@ export enum State {
2322
CLOSED // End-state, cannot change.
2423
}
2524

25+
// Quick port of djb2 to TypeScript:
26+
// http://www.cse.yorku.ca/~oz/hash.html
27+
function djb2(s: string): number {
28+
let hash = 5381;
29+
for (let i = 0; i < s.length; i++) {
30+
hash = ((hash << 5) + hash) + s.charCodeAt(i); // hash * 33 + c
31+
}
32+
return hash;
33+
}
34+
2635
export interface PeerConnection<TSignallingMessage> {
2736
// Fulfills once a connection been established with the peer.
2837
// Rejects if there is an error establishing the connection.
@@ -353,8 +362,8 @@ export class PeerConnectionClass implements PeerConnection<signals.Message> {
353362
if (state === 'have-local-offer') {
354363
return this.pc_.getLocalDescription().then(
355364
(localOffer:freedom.RTCPeerConnection.RTCSessionDescription) => {
356-
if (djb2.stringHash(JSON.stringify(remoteOffer.sdp)) <
357-
djb2.stringHash(JSON.stringify(localOffer.sdp))) {
365+
if (djb2(JSON.stringify(remoteOffer.sdp)) <
366+
djb2(JSON.stringify(localOffer.sdp))) {
358367
// TODO: implement reset and use their offer.
359368
return Promise.reject('Simultaneous offers not yet implemented.');
360369
} else {

0 commit comments

Comments
 (0)