-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_account_gen.js
75 lines (72 loc) · 1.66 KB
/
test_account_gen.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
var crypto=require('crypto');
var fs=require('fs');
var mysql=require('mysql');
var max=1000;//產生數
var count=0;
var prevCount=0;
var waiting=2;
var sp='';
var sqlFile=fs.createWriteStream('./test_account.sql');
var jsFile=fs.createWriteStream('./test_account.js');
setInterval(function(){
console.log('當前進度: %d / %d (%d 個/s)',count,max,count-prevCount);
prevCount=count;
},1000);
sqlFile
.on('open',function(){
sqlFile.write('INSERT INTO `user` (`username`,`password`,`email`,`active`,`regTime`) VALUES \n', 'utf8');
waiting--;
startGen();
})
.on('close',endGen)
;
jsFile
.on('open',function(){
jsFile.write('module.exports=[\n','utf8');
waiting--;
startGen();
})
.on('close',endGen)
;
function startGen(){
if(waiting) return;
setImmediate(gen);
}
function gen(){
if(count>=max){
sqlFile.end(';','utf8');
jsFile.end('];','utf8');
waiting=2;
return;
}
var username='test_'+fillZero(count,4);
var password=passwordHash(genPassword());
sqlFile.write(sp+mysql.format(
'(?,?,?,?,?)',
[username,password,username+'@localhost.local',true,new Date()]
)+'\n','utf8');
jsFile.write(sp+JSON.stringify({'username':username,'password':password.toString('hex')})+'\n','utf8');
sp=',';
count++;
setImmediate(gen);
}
function endGen(){
waiting--;
if(waiting) return;
console.log('完成!');
process.exit();
}
function fillZero(num,len){
num=num.toString();
while(num.length<len)
num='0'+num;
return num;
}
function genPassword(){
return crypto.randomBytes(4).toString('hex');
}
function passwordHash(password){
return crypto.createHash('sha256').update(
crypto.createHash('md5').update(password).digest()
).update(password).digest();
}