-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathotr.type.js
236 lines (199 loc) · 4.48 KB
/
otr.type.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
/**
* JavaScript OTR Types
*
* @author Khandar William
* @namespace OTR
*
* 2012-07-** initial commit, define custom types, create MPI and Data, Pubkey and Sig,
* readPubkey and readSig, Sig.toBytes will return 2*20 bytes, add compareTo,
* compareTo now use biginteger
* 2012-08-02 add TLV
*/
Otr.Type = (function () {
"use strict";
/**
* Data holds an array of bytes
* @param {Array} x Array of bytes
*/
function Data(x) {
this.value = x;
}
Data.prototype = {
value: [],
getLength: function () {
return this.value.length;
},
getValue: function () {
return this.value;
},
equals: function (d) {
var i, len = this.getLength();
if (this.getLength() !== d.getLength()) return false;
for (i=0; i<len; ++i) {
if (this.value[i] !== d.value[i]) return false;
}
return true;
},
// Returns array of byte values
toBytes: function () {
var bytes = this.value.slice(0),
len = this.getLength();
bytes.unshift(
(len >> 24) & 0xFF,
(len >> 16) & 0xFF,
(len >> 8) & 0xFF,
(len) & 0xFF
);
return bytes;
}
};
/**
* MPI holds an array of bytes which represents a BigInteger value
* @param {Array|BigInteger} x Array of bytes OR BigInteger
*/
function MPI(x) {
if (x instanceof Otr.BigInteger) {
this.value = x.toByteArray();
} else {
this.value = x;
}
while (this.value[0] === 0) { // kadang ada padding 0 di awal
this.value.shift();
}
}
MPI.prototype = {
value: [],
getLength: function () {
return this.value.length;
},
getValue: function () {
return this.value;
},
equals: function (m) {
var i, len = this.getLength();
if (this.getLength() !== m.getLength()) return false;
for (i=0; i<len; ++i) {
if (this.value[i] !== m.value[i]) return false;
}
return true;
},
// @return < 0 if this < mpi; > 0 if this > mpi; 0 if this == mpi
compareTo: function (mpi) {
return this.toBigInteger().compareTo(mpi.toBigInteger());
},
// Returns array of byte values
toBytes: function () {
var bytes = this.value.slice(0),
len = this.getLength();
bytes.unshift(
(len >> 24) & 0xFF,
(len >> 16) & 0xFF,
(len >> 8) & 0xFF,
(len) & 0xFF
);
return bytes;
},
// Returns Otr.BigInteger value
toBigInteger: function () {
return Otr.BigInteger.fromMagnitude(1, this.value);
}
};
/**
* Pubkey holds information about a DSA public key
* @param {MPI} p, q, g, y
*/
function Pubkey(p, q, g, y) {
// all MPI
this.p = p;
this.q = q;
this.g = g;
this.y = y;
}
Pubkey.prototype = {
type: 0x0000, // Short
toBytes: function () {
var bytes = [];
// append type
bytes.push((this.type >> 8) & 0xFF);
bytes.push(this.type & 0xFF);
// append p, q, g, y
bytes = bytes.concat(
this.p.toBytes(),
this.q.toBytes(),
this.g.toBytes(),
this.y.toBytes()
);
return bytes;
}
};
// Static: Read a Pubkey from ByteBuffer
// @param {ByteBuffer} buf
Pubkey.readPubkey = function (buf) {
var type = buf.readShort(),
p = buf.readMPI(),
q = buf.readMPI(),
g = buf.readMPI(),
y = buf.readMPI();
return new Pubkey(p, q, g, y);
};
/**
* Sig holds information about a DSA signature
* @param {BigInteger} r, s
*/
function Sig(r, s) {
// all BigInteger
this.r = r;
this.s = s;
}
Sig.prototype = {
toBytes: function () {
var bar = this.r.toByteArray(),
bas = this.s.toByteArray();
// byte length must be 20
while (bar[0] === 0) bar.shift();
while (bar.length < 20) bar.unshift(0x00);
while (bas[0] === 0) bas.shift();
while (bas.length < 20) bas.unshift(0x00);
return bar.concat(bas);
}
};
// Static: Read a Sig from ByteBuffer
// @param {ByteBuffer} buf
Sig.readSig = function (buf) {
var r = buf.readBytes(20),
s = buf.readBytes(20);
return new Sig(Otr.BigInteger.fromMagnitude(1, r), Otr.BigInteger.fromMagnitude(1, s));
};
/**
* TLV record
* @param {Short} type
* @param {Short} len
* @param {Array} val array of bytes
*/
function TLV(type, len, val) {
this.type = type;
this.len = len;
this.val = val;
}
TLV.prototype = {
};
// TLV Types
TLV.PADDING = 0;
TLV.DISCONNECTED = 1;
// Static: Read a TLV from ByteBuffer
// @param {ByteBuffer} buf
TLV.readTLV = function (buf) {
var type = buf.readShort(),
len = buf.readShort(),
val = buf.readBytes(len);
return new TLV(type, len, val);
};
var Type = {
Data: Data,
MPI: MPI,
Pubkey: Pubkey,
Sig: Sig,
TLV: TLV
};
return Type;
}());