refactor: refactor of libs

This commit is contained in:
Савелий Савенок 2024-11-09 16:48:52 +03:00
parent b67cddf4eb
commit 40f6ae7f76
15 changed files with 46 additions and 176 deletions

@ -1,4 +1,4 @@
Copyright (c) 2011, Chris Umbel Copyright (c) 2023, Chris Umbel, Lisoveliy
Permission is hereby granted, free of charge, to any person obtaining a copy Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal of this software and associated documentation files (the "Software"), to deal

10
lib/base32/README.md Normal file

@ -0,0 +1,10 @@
# base32
Implementation of RFC 3548 Base32 encoding/decoding for zepp quickjs.
## Usage
import {encode, decode} from 'base32/index.js'
base32.encode('node');
// output: NZXWIZI=
base32.decode('NZXWIZI=');
//output: node

@ -1,5 +1,5 @@
/* /*
Copyright (c) 2011, Chris Umbel Copyright (c) 2023, Chris Umbel, Lisoveliy
Permission is hereby granted, free of charge, to any person obtaining a copy Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal of this software and associated documentation files (the "Software"), to deal
@ -20,4 +20,4 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE. THE SOFTWARE.
*/ */
export {encode, decode} from './lib/thirty-two/thirty-two.js' export { encode, decode } from './lib/base32/base32.js'

@ -1,5 +1,5 @@
/* /*
Copyright (c) 2011, Chris Umbel Copyright (c) 2023, Chris Umbel, Lisoveliy
Permission is hereby granted, free of charge, to any person obtaining a copy Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal of this software and associated documentation files (the "Software"), to deal
@ -37,12 +37,12 @@ var byteTable = [
function quintetCount(buff) { function quintetCount(buff) {
var quintets = Math.floor(buff.length / 5); var quintets = Math.floor(buff.length / 5);
return buff.length % 5 === 0 ? quintets: quintets + 1; return buff.length % 5 === 0 ? quintets : quintets + 1;
} }
export function encode(plain) { export function encode(plain) {
if(!Buffer.isBuffer(plain)){ if (!Buffer.isBuffer(plain)) {
plain = new Buffer(plain); plain = new Buffer(plain);
} }
var i = 0; var i = 0;
var j = 0; var j = 0;
@ -52,10 +52,10 @@ export function encode(plain) {
/* byte by byte isn't as pretty as quintet by quintet but tests a bit /* byte by byte isn't as pretty as quintet by quintet but tests a bit
faster. will have to revisit. */ faster. will have to revisit. */
while(i < plain.length) { while (i < plain.length) {
var current = plain[i]; var current = plain[i];
if(shiftIndex > 3) { if (shiftIndex > 3) {
digit = current & (0xff >> shiftIndex); digit = current & (0xff >> shiftIndex);
shiftIndex = (shiftIndex + 5) % 8; shiftIndex = (shiftIndex + 5) % 8;
digit = (digit << shiftIndex) | ((i + 1 < plain.length) ? digit = (digit << shiftIndex) | ((i + 1 < plain.length) ?
@ -64,14 +64,14 @@ export function encode(plain) {
} else { } else {
digit = (current >> (8 - (shiftIndex + 5))) & 0x1f; digit = (current >> (8 - (shiftIndex + 5))) & 0x1f;
shiftIndex = (shiftIndex + 5) % 8; shiftIndex = (shiftIndex + 5) % 8;
if(shiftIndex === 0) i++; if (shiftIndex === 0) i++;
} }
encoded[j] = charTable.charCodeAt(digit); encoded[j] = charTable.charCodeAt(digit);
j++; j++;
} }
for(i = j; i < encoded.length; i++) { for (i = j; i < encoded.length; i++) {
encoded[i] = 0x3d; //'='.charCodeAt(0) encoded[i] = 0x3d; //'='.charCodeAt(0)
} }
@ -83,27 +83,27 @@ export function decode(encoded) {
var plainDigit = 0; var plainDigit = 0;
var plainChar; var plainChar;
var plainPos = 0; var plainPos = 0;
if(!Buffer.isBuffer(encoded)){ if (!Buffer.isBuffer(encoded)) {
encoded = new Buffer(encoded); encoded = new Buffer(encoded);
} }
var decoded = new Buffer(Math.ceil(encoded.length * 5 / 8)); var decoded = new Buffer(Math.ceil(encoded.length * 5 / 8));
/* byte by byte isn't as pretty as octet by octet but tests a bit /* byte by byte isn't as pretty as octet by octet but tests a bit
faster. will have to revisit. */ faster. will have to revisit. */
for(var i = 0; i < encoded.length; i++) { for (var i = 0; i < encoded.length; i++) {
if(encoded[i] === 0x3d){ //'=' if (encoded[i] === 0x3d) { //'='
break; break;
} }
var encodedByte = encoded[i] - 0x30; var encodedByte = encoded[i] - 0x30;
if(encodedByte < byteTable.length) { if (encodedByte < byteTable.length) {
plainDigit = byteTable[encodedByte]; plainDigit = byteTable[encodedByte];
if(shiftIndex <= 3) { if (shiftIndex <= 3) {
shiftIndex = (shiftIndex + 5) % 8; shiftIndex = (shiftIndex + 5) % 8;
if(shiftIndex === 0) { if (shiftIndex === 0) {
plainChar |= plainDigit; plainChar |= plainDigit;
decoded[plainPos] = plainChar; decoded[plainPos] = plainChar;
plainPos++; plainPos++;
@ -120,7 +120,7 @@ export function decode(encoded) {
plainChar = 0xff & (plainDigit << (8 - shiftIndex)); plainChar = 0xff & (plainDigit << (8 - shiftIndex));
} }
} else { } else {
throw new Error('Invalid input - it is not base32 encoded string'); throw new Error('Invalid input - it is not base32 encoded string');
} }
} }

@ -1,13 +1,10 @@
{ {
"name": "thirty-two", "name": "base32",
"description": "Implementation RFC 3548 Base32 encoding/decoding for node.", "description": "Implementation RFC 3548 Base32 encoding/decoding for node.",
"version": "1.0.2", "version": "1.0.2-zepp",
"engines": {
"node": ">=0.2.6"
},
"author": "Chris Umbel <chris@chrisumbel.com>", "author": "Chris Umbel <chris@chrisumbel.com>",
"keywords": ["base32", "encoding"], "keywords": ["base32", "encoding"],
"main": "./lib/thirty-two/index.js", "main": "index.js",
"repository": { "repository": {
"type": "git", "type": "git",
"url": "git://github.com/chrisumbel/thirty-two.git" "url": "git://github.com/chrisumbel/thirty-two.git"

@ -1,4 +0,0 @@
*.kpf
*~
\#*
node_modules

@ -1,24 +0,0 @@
# Copyright (c) 2011, Chris Umbel
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
SHELL := /bin/bash
test:
jasmine-node spec/

@ -1,15 +0,0 @@
# thirty-two
Implementation of RFC 3548 Base32 encoding/decoding for node.
## Installation
npm install thirty-two
## Usage
var base32 = require('thirty-two');
base32.encode('node');
// output: NZXWIZI=
base32.decode('NZXWIZI=');
//output: node

@ -1,63 +0,0 @@
/*
Copyright (c) 2011, Chris Umbel
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
if(!expect){
function expect(a){
return {
toBe: function(b){
require('assert').strictEqual(a, b);
}
};
}
}
var base32 = require('../lib/thirty-two/');
describe('thirty-two', function() {
it('should encode', function() {
expect(base32.encode('a').toString()).toBe('ME======');
expect(base32.encode('be').toString()).toBe('MJSQ====');
expect(base32.encode('bee').toString()).toBe('MJSWK===');
expect(base32.encode('beer').toString()).toBe('MJSWK4Q=');
expect(base32.encode('beers').toString()).toBe('MJSWK4TT');
expect(base32.encode('beers 1').toString()).toBe('MJSWK4TTEAYQ====');
expect(base32.encode('shockingly dismissed').toString()).toBe('ONUG6Y3LNFXGO3DZEBSGS43NNFZXGZLE');
});
it('should decode', function() {
expect(base32.decode('ME======').toString()).toBe('a');
expect(base32.decode('MJSQ====').toString()).toBe('be');
expect(base32.decode('ONXW4===').toString()).toBe('son');
expect(base32.decode('MJSWK===').toString()).toBe('bee');
expect(base32.decode('MJSWK4Q=').toString()).toBe('beer');
expect(base32.decode('MJSWK4TT').toString()).toBe('beers');
expect(base32.decode('mjswK4TT').toString()).toBe('beers');
expect(base32.decode('MJSWK4TTN5XA====').toString()).toBe('beerson');
expect(base32.decode('MJSWK4TTEAYQ====').toString()).toBe('beers 1');
expect(base32.decode('ONUG6Y3LNFXGO3DZEBSGS43NNFZXGZLE').toString()).toBe('shockingly dismissed');
});
it('should be binary safe', function() {
expect(base32.decode(base32.encode(new Buffer([0x00, 0xff, 0x88]))).toString('hex')).toBe('00ff88');
expect(base32.encode(new Buffer("f61e1f998d69151de8334dbe753ab17ae831c13849a6aecd95d0a4e5dc25", 'hex')).toString()).toBe('6YPB7GMNNEKR32BTJW7HKOVRPLUDDQJYJGTK5TMV2CSOLXBF');
expect(base32.decode('6YPB7GMNNEKR32BTJW7HKOVRPLUDDQJYJGTK5TMV2CSOLXBF').toString('hex')).toBe('f61e1f998d69151de8334dbe753ab17ae831c13849a6aecd95d0a4e5dc25');
});
});

@ -1,3 +0,0 @@
language: node_js
node_js:
- "8"

@ -1,5 +0,0 @@
# totp.js
Two-factor authentication implementation in pure javascript. One-time password generator (HOTP/TOTP) with support for Google Authenticator.
[![Build Status](https://travis-ci.org/wuyanxin/totp.js.svg?branch=master)](https://travis-ci.org/wuyanxin/totp.js)

@ -1,9 +1,2 @@
/* export { HOTP } from './lib/hotp.js'
* @Author: wuyanxin export { TOTP } from './lib/totp.js'
* @Date: 2018-03-21 23:12:14
* @Last Modified by: wuyanxin
* @Last Modified time: 2018-03-21 23:12:14
*/
export {HOTP} from './lib/hotp.js'
export {TOTP} from './lib/totp.js'

@ -1,11 +1,4 @@
/* import { encode, decode } from '../../base32/index.js'
* @Author: wuyanxin
* @Date: 2018-03-21 22:25:37
* @Last Modified by: wuyanxin
* @Last Modified time: 2018-03-21 22:29:43
*/
import {encode, decode} from '../../thirty-two/index.js'
import jsSHA from 'jssha' import jsSHA from 'jssha'
export class HOTP { export class HOTP {
@ -54,11 +47,11 @@ export class HOTP {
} }
_truncat(hmac_result) { _truncat(hmac_result) {
const offset = hmac_result[19].charCodeAt() & 0xf; const offset = hmac_result[19].charCodeAt() & 0xf;
const bin_code = (hmac_result[offset].charCodeAt() & 0x7f) << 24 const bin_code = (hmac_result[offset].charCodeAt() & 0x7f) << 24
| (hmac_result[offset+1].charCodeAt() & 0xff) << 16 | (hmac_result[offset + 1].charCodeAt() & 0xff) << 16
| (hmac_result[offset+2].charCodeAt() & 0xff) << 8 | (hmac_result[offset + 2].charCodeAt() & 0xff) << 8
| (hmac_result[offset+3].charCodeAt() & 0xff); | (hmac_result[offset + 3].charCodeAt() & 0xff);
let otp = (bin_code % 10 ** this.digit).toString(); let otp = (bin_code % 10 ** this.digit).toString();
while (otp.length < this.digit) { while (otp.length < this.digit) {
otp = '0' + otp; otp = '0' + otp;

@ -1,14 +1,7 @@
/* import { HOTP } from "./hotp.js";
* @Author: wuyanxin
* @Date: 2018-03-21 22:25:42
* @Last Modified by: wuyanxin
* @Last Modified time: 2018-03-21 22:42:28
*/
import {HOTP} from "./hotp.js";
export class TOTP extends HOTP { export class TOTP extends HOTP {
constructor(key){ constructor(key) {
super(key) super(key)
} }
@ -20,5 +13,4 @@ export class TOTP extends HOTP {
verify(otp, timeStep = 30, t0 = 0) { verify(otp, timeStep = 30, t0 = 0) {
return otp === this.genOTP(timeStep, t0); return otp === this.genOTP(timeStep, t0);
} }
} }

@ -1,6 +1,6 @@
{ {
"name": "totp.js", "name": "totp.js",
"version": "0.0.1", "version": "1.0.1-zepp",
"description": "Two-factor authentication implementation in pure javascript. One-time password generator (HOTP/TOTP) with support for Google Authenticator. ", "description": "Two-factor authentication implementation in pure javascript. One-time password generator (HOTP/TOTP) with support for Google Authenticator. ",
"main": "index.js", "main": "index.js",
"scripts": { "scripts": {
@ -24,8 +24,7 @@
}, },
"homepage": "https://github.com/wuyanxin/totp.js#readme", "homepage": "https://github.com/wuyanxin/totp.js#readme",
"dependencies": { "dependencies": {
"jssha": "^2.3.1", "jssha": "^2.3.1"
"thirty-two": "^1.0.2"
}, },
"devDependencies": { "devDependencies": {
"mocha": "^5.0.4" "mocha": "^5.0.4"