1
1
var Buffer = require ( 'buffer' ) . Buffer ;
2
2
var crypto = require ( 'crypto' ) ;
3
+ var Auth = exports ;
3
4
4
5
function sha1 ( msg ) {
5
6
var hash = crypto . createHash ( 'sha1' ) ;
6
7
hash . update ( msg ) ;
7
8
// hash.digest() does not output buffers yet
8
9
return hash . digest ( 'binary' ) ;
9
10
} ;
10
- exports . sha1 = sha1 ;
11
+ Auth . sha1 = sha1 ;
11
12
12
13
function xor ( a , b ) {
13
14
a = new Buffer ( a , 'binary' ) ;
@@ -18,9 +19,9 @@ function xor(a, b) {
18
19
}
19
20
return result ;
20
21
} ;
21
- exports . xor = xor ;
22
+ Auth . xor = xor ;
22
23
23
- exports . token = function ( password , scramble ) {
24
+ Auth . token = function ( password , scramble ) {
24
25
if ( ! password ) {
25
26
return new Buffer ( 0 ) ;
26
27
}
@@ -33,7 +34,7 @@ exports.token = function(password, scramble) {
33
34
34
35
// This is a port of sql/password.c:hash_password which needs to be used for
35
36
// pre-4.1 passwords.
36
- exports . hashPassword = function ( password ) {
37
+ Auth . hashPassword = function ( password ) {
37
38
var nr = [ 0x5030 , 0x5735 ] ,
38
39
add = 7 ,
39
40
nr2 = [ 0x1234 , 0x5671 ] ,
@@ -68,7 +69,7 @@ exports.hashPassword = function(password) {
68
69
return result ;
69
70
} ;
70
71
71
- exports . randomInit = function ( seed1 , seed2 ) {
72
+ Auth . randomInit = function ( seed1 , seed2 ) {
72
73
return {
73
74
max_value : 0x3FFFFFFF ,
74
75
max_value_dbl : 0x3FFFFFFF ,
@@ -77,14 +78,14 @@ exports.randomInit = function(seed1, seed2) {
77
78
} ;
78
79
} ;
79
80
80
- exports . myRnd = function ( r ) {
81
+ Auth . myRnd = function ( r ) {
81
82
r . seed1 = ( r . seed1 * 3 + r . seed2 ) % r . max_value ;
82
83
r . seed2 = ( r . seed1 + r . seed2 + 33 ) % r . max_value ;
83
84
84
85
return r . seed1 / r . max_value_dbl ;
85
86
} ;
86
87
87
- exports . scramble323 = function ( message , password ) {
88
+ Auth . scramble323 = function ( message , password ) {
88
89
var to = new Buffer ( 8 ) ,
89
90
hashPass = this . hashPassword ( password ) ,
90
91
hashMessage = this . hashPassword ( message . slice ( 0 , 8 ) ) ,
@@ -104,7 +105,7 @@ exports.scramble323 = function(message, password) {
104
105
return to ;
105
106
} ;
106
107
107
- exports . fmt32 = function ( x ) {
108
+ Auth . fmt32 = function ( x ) {
108
109
var a = x [ 0 ] . toString ( 16 ) ,
109
110
b = x [ 1 ] . toString ( 16 ) ;
110
111
@@ -117,18 +118,18 @@ exports.fmt32 = function(x){
117
118
return '' + a + '/' + b ;
118
119
} ;
119
120
120
- exports . xor32 = function ( a , b ) {
121
+ Auth . xor32 = function ( a , b ) {
121
122
return [ a [ 0 ] ^ b [ 0 ] , a [ 1 ] ^ b [ 1 ] ] ;
122
123
} ;
123
124
124
- exports . add32 = function ( a , b ) {
125
+ Auth . add32 = function ( a , b ) {
125
126
var w1 = a [ 1 ] + b [ 1 ] ,
126
127
w2 = a [ 0 ] + b [ 0 ] + ( ( w1 & 0xFFFF0000 ) >> 16 ) ;
127
128
128
129
return [ w2 & 0xFFFF , w1 & 0xFFFF ] ;
129
130
} ;
130
131
131
- exports . mul32 = function ( a , b ) {
132
+ Auth . mul32 = function ( a , b ) {
132
133
// based on this example of multiplying 32b ints using 16b
133
134
// http://www.dsprelated.com/showmessage/89790/1.php
134
135
var w1 = a [ 1 ] * b [ 1 ] ,
@@ -137,26 +138,26 @@ exports.mul32 = function(a,b){
137
138
return [ w2 & 0xFFFF , w1 & 0xFFFF ] ;
138
139
} ;
139
140
140
- exports . and32 = function ( a , b ) {
141
+ Auth . and32 = function ( a , b ) {
141
142
return [ a [ 0 ] & b [ 0 ] , a [ 1 ] & b [ 1 ] ] ;
142
143
} ;
143
144
144
- exports . shl32 = function ( a , b ) {
145
+ Auth . shl32 = function ( a , b ) {
145
146
// assume b is 16 or less
146
147
var w1 = a [ 1 ] << b ,
147
148
w2 = ( a [ 0 ] << b ) | ( ( w1 & 0xFFFF0000 ) >> 16 ) ;
148
149
149
150
return [ w2 & 0xFFFF , w1 & 0xFFFF ] ;
150
151
} ;
151
152
152
- exports . int31Write = function ( buffer , number , offset ) {
153
+ Auth . int31Write = function ( buffer , number , offset ) {
153
154
buffer [ offset ] = ( number [ 0 ] >> 8 ) & 0x7F ;
154
155
buffer [ offset + 1 ] = ( number [ 0 ] ) & 0xFF ;
155
156
buffer [ offset + 2 ] = ( number [ 1 ] >> 8 ) & 0xFF ;
156
157
buffer [ offset + 3 ] = ( number [ 1 ] ) & 0xFF ;
157
158
} ;
158
159
159
- exports . int32Read = function ( buffer , offset ) {
160
+ Auth . int32Read = function ( buffer , offset ) {
160
161
return ( buffer [ offset ] << 24 )
161
162
+ ( buffer [ offset + 1 ] << 16 )
162
163
+ ( buffer [ offset + 2 ] << 8 )
0 commit comments