forked from omatztw/tkmninja
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtripcode.js
More file actions
27 lines (24 loc) · 887 Bytes
/
Copy pathtripcode.js
File metadata and controls
27 lines (24 loc) · 887 Bytes
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
let crypt = null;
try {
crypt = require('unix-crypt-td-js');
} catch (e) {
crypt = null;
}
function sanitizeSalt(s) {
return String(s || '')
.replace(/[\x00-\x20:;<=>?@[\\\]^_`]/g, '.')
.replace(/[^.\/0-9A-Za-z]/g, '.');
}
function createTrip(source) {
source = String(source || '');
const salt = sanitizeSalt((source + 'H.').slice(1, 3));
if (crypt) {
const result = typeof crypt === 'function' ? crypt(source, salt) : crypt.crypt(source, salt);
return String(result).slice(-10);
}
// Fallback:旧実装互換ではないが、依存が無い環境でもログインを止めない。
// 2ch互換にするには package.json の unix-crypt-td-js を install すること。
const crypto = require('crypto');
return crypto.createHash('sha1').update(source).digest('base64').replace(/[+=/]/g, '').slice(0, 10);
}
module.exports = { createTrip };