-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsecrethelper.js
134 lines (115 loc) · 2.96 KB
/
secrethelper.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
"use strict";
// let base58 = require('base-58');
let crypto = require('crypto');
class SecretHelper {
/**
* base64 decode
* @param base64str
* @returns {Buffer}
* @constructor
*/
static FromBase64StringToBytes(base64str) {
return new Buffer(base64str, "base64");
}
/**
* base64 decode
* @param base64str
* @returns {string}
* @constructor
*/
static FromBase64String(base64str) {
let bytes = SecretHelper.FromBase64StringToBytes(base64str);
return bytes.toString();
}
static FromUrlBase64String(urlBase64Str) {
return decodeURI(SecretHelper.FromBase64String(urlBase64Str));
}
static ToBase64String(source) {
let bytes = new Buffer(source);
return bytes.toString("base64");
}
static ToBase64StringFromBytes(source) {
return source.toString("base64");
}
static ToUrlBase64String(source) {
return SecretHelper.ToBase64String(encodeURI(source));
}
static ToMD5(source) {
var crypto = require("crypto");
return crypto.createHash("md5").update(source).digest("hex").toUpperCase();
}
/**
* sha1 hash
* @param source
* @returns {string}
* @constructor
*/
static ToSHA1(source) {
var crypto = require("crypto");
return crypto.createHash("sha1").update(source).digest("hex").toUpperCase();
}
static NewPassword(len) {
let char = "AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz0123456789!?#@*&.,;:+-=()[]_";
let pwd = "";
if (len == null) {
len = 10;
}
while (len--) {
pwd += char[SecretHelper.random(0, char.length - 1)];
}
return pwd;
}
static NewID(len) {
let char = "AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz0123456789_";
let pwd = "";
if (len == null) {
len = 10;
}
while (len--) {
pwd += char[SecretHelper.random(0, char.length - 1)];
}
return pwd;
}
/**
* 随机数
* @param min
* @param max
* @returns {*}
*/
static random(min, max) {
return min + parseInt(Math.random() * (max - min + 1), 10);
}
// /**
// * base58编码
// * @param buf
// * @returns {*}
// */
// static base58encode(buf) {
// return base58.encode(buf);
// }
//
// /**
// * base58编码
// * @param buf
// * @returns {*}
// */
// static base58decode(str) {
// return base58.decode(str);
// }
/**
* sha256hash
* @param buf
*/
static sha256hash(buf) {
return crypto.createHash("sha256").update(buf).digest();
}
/**
* ripemd160 hash
* @param buf
* @returns {PromiseLike<ArrayBuffer>}
*/
static ripemd160hash(buf) {
return crypto.createHash("ripemd160").update(buf).digest();
}
}
module.exports = SecretHelper;