fix: api请求验证

This commit is contained in:
chaosBreaking
2019-09-29 23:10:13 +08:00
parent 624f34e1ed
commit f0115b3c26
3 changed files with 19 additions and 9 deletions

View File

@@ -66,7 +66,7 @@ module.exports = {
let outputEncoding = (option.output === 'buf') ? undefined : (my.OUTPUT_LIST.indexOf(option.output) >= 0 ? option.output : my.OUTPUT); // 'latin1', 'base64', 'hex' by default or 'buf' to Buffer explicitly
let cipher = crypto.createCipher(
my.CIPHER_LIST.indexOf(option.cipher) >= 0 ? option.cipher : my.CIPHER,
this.hash(pwd));
this.hash(pwd, option)); //哈希加盐
if (typeof (data) !== 'string' && !(data instanceof Buffer) && !(data instanceof DataView))
data = JSON.stringify(data);
let encrypted = cipher.update(data, inputEncoding, outputEncoding);
@@ -83,15 +83,13 @@ module.exports = {
let outputEncoding = (option.output === 'buf') ? undefined : (my.INPUT_LIST.indexOf(option.output) >= 0 ? option.output : my.INPUT); // output (=input of encrypt) could be 'latin1', 'ascii', 'utf8' by default or 'buf' to Buffer explicitly
let decipher = crypto.createDecipher(
my.CIPHER_LIST.indexOf(option.cipher) >= 0 ? option.cipher : my.CIPHER,
this.hash(pwd));
this.hash(pwd, option)); // 哈希加盐
let decrypted = decipher.update(data, inputEncoding, outputEncoding);
decrypted += decipher.final(outputEncoding); // 但是 Buffer + Buffer 还是会变成string
if (option.format === 'json') { // 如果用户输入错误密码deciper也能返回结果。为了判断是否正确结果对应当是 json 格式的原文做解析来验证。
try {
JSON.parse(decrypted);
} catch (exception) {
return null;
}
try {
JSON.parse(decrypted);
} catch (exception) {
return null;
}
return decrypted;
}