node: decode(), simplified code

This commit is contained in:
selfisekai 2020-03-24 00:34:13 +01:00
parent 33fd4f1cd4
commit 1d571d4680
3 changed files with 18 additions and 17 deletions

4
node/src/index.d.ts vendored
View file

@ -1,4 +1,4 @@
declare module '@wulkanowy/qr-node' {
export function encode(password: string, content: string): string;
// export function decode(password: string, content: string): string;
}
export function decode(password: string, content: string): string;
}

View file

@ -7,27 +7,21 @@ const BASE64 = 'base64';
const toArrayBuffer = (str) => new TextEncoder().encode(str);
const encode = (password, content) => {
const pass = toArrayBuffer(password);
const cont = toArrayBuffer(content);
const cipher = crypto.createCipheriv(ALGORITHM, pass, Buffer.alloc(0));
let encrypted = cipher.update(cont, UTF8, BASE64);
const cipher = crypto.createCipheriv(ALGORITHM, toArrayBuffer(password), Buffer.alloc(0));
let encrypted = cipher.update(content, UTF8, BASE64);
encrypted += cipher.final(BASE64);
return encrypted;
};
/*
const decode = (password, content) => {
const pass = toArrayBuffer(password);
const cont = toArrayBuffer(content);
const decipher = crypto.createDecipheriv(ALGORITHM, pass, Buffer.alloc(0));
let decrypted = decipher.update(cont, BASE64, UTF8);
const decipher = crypto.createDecipheriv(ALGORITHM, toArrayBuffer(password), Buffer.alloc(0));
let decrypted = decipher.update(content, BASE64, UTF8);
decrypted += decipher.final(UTF8);
return decrypted;
};
*/
module.exports = {
encode,
// decode,
decode,
__esModule: true,
};

View file

@ -1,13 +1,20 @@
const vulcanQR = require('../index');
const assert = require('assert');
const password = '0123456789ABCDEF';
const content = 'CERT#https://api.fakelog.cf/Default/mobile-api#FK100000#ENDCERT';
const base64 = '27Grk6d8ZDely5eeF7j2ngWrWV9eZa5Dz9ZmuiBavysDp74TCr6EHJOs6TaIXFh3HsROWSM11pv3cPvRGSi7Nw==';
describe('encoder', () => {
it('encodes', () => {
const password = '0123456789ABCDEF';
const content = 'CERT#https://api.fakelog.cf/Default/mobile-api#FK100000#ENDCERT';
const base64 = '27Grk6d8ZDely5eeF7j2ngWrWV9eZa5Dz9ZmuiBavysDp74TCr6EHJOs6TaIXFh3HsROWSM11pv3cPvRGSi7Nw==';
const result = vulcanQR.encode(password, content);
assert.equal(result, base64);
});
});
describe('decoder', () => {
it('decodes', () => {
const result = vulcanQR.decode(password, base64);
assert.equal(result, content);
});
});