-
Notifications
You must be signed in to change notification settings - Fork 11
/
util.js
126 lines (111 loc) · 2.79 KB
/
util.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
/* Utility Functions */
/* Many of these functions maybe poorly written/implemented as they were
originally meant for one challenge and I was too lazy to rewrite them properly. :)
This is a poor copy of saelo's Int64 library which can be found here -
https://github.com/saelo/jscpwn/blob/master/utils.js
*/
String.prototype.rjust = function rjust(n,chr){
chr = chr || '0'
if(this.length>n)
return this.toString();
return (chr.repeat(n)+this.toString()).slice(-1*n);
}
String.prototype.ljust = function ljust(n,chr){
chr = chr || '0'
if(this.length>n)
return this.toString();
return (this.toString()+chr.repeat(n)).slice(0,n);
}
String.prototype.hexdecode = function hexdecode(){
inp=this.toString();
if (this.length%2 !=0)
inp='0'+inp.toString();
out=[];
for(var i=0;i<inp.length;i+=2)
out.push(parseInt(inp.substr(i,2),16));
return out;
}
function print1(num){
rep='';
for(var i=0;i<8;i++){
rep+=num[i].toString(16).rjust(2);
}
console.log("0x"+rep.rjust(16));
// document.getElementById("demo").innerText += "0x"+rep.rjust(16) + '\n';
}
function data(inp){
bytes='';
if ( (typeof inp) == 'string'){
inp=inp.replace("0x",'');
inp=inp.rjust(16);
bytes=new Uint8Array(inp.hexdecode());
}
else if (typeof inp == 'number'){
bytes=new Uint8Array(new Float64Array([inp]).buffer);
bytes.reverse();
}
else if (typeof inp == 'object'){
bytes=new Uint8Array(8);
bytes.set(inp);
bytes.reverse();
}
return bytes;
}
function inttod(num){
num.reverse();
temp = new Float64Array(num.buffer)[0];
num.reverse();
return temp;
}
function dtoint(num){
int=new Uint32Array(new Float64Array([num]).buffer)
// console.log(int[1].toString(16)+int[0].toString(16));
return int;
}
function RS(inp,amt){
amt = amt || 1;
num='';
for(var i=0;i<8;i++){
num+=inp[i].toString(2).rjust(8);
}
num=num.slice(0,-1*amt);
num=num.rjust(64);
num=parseInt(num,2).toString(16).rjust(16);
for(var i=0,j=0;i<num.length;i+=2,j++){
inp[j]=parseInt(num.substr(i,2),16);
}
return inp;
}
function LS(inp,amt){
amt = amt || 1;
num='';
for(var i=0;i<8;i++){
num+=inp[i].toString(2).rjust(8);
}
num=num.slice(amt);
num=num.ljust(64);
num=parseInt(num,2).toString(16).rjust(16);
for(var i=0,j=0;i<num.length;i+=2,j++){
inp[j]=parseInt(num.substr(i,2),16);
}
return inp;
}
function sub(inp1,inp2){
carry=0;
for(var i=inp1.length-1;i>=0;i--){
diff=inp1[i]-inp2[i]-carry;
carry=diff<0|0;
inp1[i]=diff;
}
return inp1;
}
function add(inp1,inp2){
carry=0;
for(var i=inp1.length-1;i>=0;i--){
sum=inp1[i]+inp2[i]+carry;
carry=sum/0x100;
inp1[i]=(sum%0x100);
}
return inp1;
}
/* Utility functions end */