Skip to content

Commit

Permalink
Add JsSIP.Utils.createRandomToken utility to create random tokens
Browse files Browse the repository at this point in the history
  • Loading branch information
jmillan committed Feb 14, 2013
1 parent bc21816 commit dcf4021
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/DigestAuthentication.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ JsSIP.DigestAuthentication.prototype.authenticate = function(password) {

password = password || this.password;

this.cnonce = Math.random().toString(36).substr(2, 12);
this.cnonce = JsSIP.Utils.createRandomToken(12);
this.nc += 1;

// nc-value = 8LHEX. Max value = 'FFFFFFFF'
Expand Down
2 changes: 1 addition & 1 deletion src/Registrator.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ JsSIP.Registrator = function(ua, transport) {
this.min_expires = ua.configuration.register_min_expires;

// Call-ID and CSeq values RFC3261 10.2
this.call_id = Math.random().toString(36).substr(2, 22);
this.call_id = JsSIP.Utils.createRandomToken(22);
this.cseq = 80;

this.registrar = 'sip:'+ ua.configuration.domain;
Expand Down
2 changes: 1 addition & 1 deletion src/SIPMessage.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ JsSIP.OutgoingRequest = function(method, ruri, ua, params, extraHeaders, body) {
if(params.call_id) {
call_id = params.call_id;
} else {
call_id = ua.configuration.jssip_id + Math.random().toString(36).substr(2, 15);
call_id = ua.configuration.jssip_id + JsSIP.Utils.createRandomToken(15);
}
this.setHeader('call-id', call_id);

Expand Down
4 changes: 2 additions & 2 deletions src/UA.js
Original file line number Diff line number Diff line change
Expand Up @@ -600,7 +600,7 @@ JsSIP.UA.prototype.loadConfig = function(configuration) {
/* Host address
* Value to be set in Via sent_by and host part of Contact FQDN
*/
via_host: Math.random().toString(36).substr(2, 12) + '.invalid',
via_host: JsSIP.Utils.createRandomToken(12) + '.invalid',

// Password
password: null,
Expand Down Expand Up @@ -682,7 +682,7 @@ JsSIP.UA.prototype.loadConfig = function(configuration) {
settings.instance_id = JsSIP.Utils.newUUID();

// jssip_id instance parameter. Static random tag of length 5
settings.jssip_id = Math.random().toString(36).substr(2, 5);
settings.jssip_id = JsSIP.Utils.createRandomToken(5);

settings.from_uri = settings.uri.toAor();

Expand Down
16 changes: 15 additions & 1 deletion src/Utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,22 @@ JsSIP.Utils= {
return !isNaN(num) && (parseFloat(num) === parseInt(num,10));
},

createRandomToken: function(size, base) {
var i, r,
token = '';

base = base || 32;

for( i=0; i < size; i++ ) {
r = Math.random() * base|0;
token += r.toString(base);
}

return token;
},

newTag: function() {
return Math.random().toString(36).substr(2,JsSIP.C.TAG_LENGTH);
return JsSIP.Utils.createRandomToken(JsSIP.C.TAG_LENGTH);
},

// http://stackoverflow.com/users/109538/broofa
Expand Down

0 comments on commit dcf4021

Please sign in to comment.