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

View File

@ -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
of this software and associated documentation files (the "Software"), to deal

10
lib/base32/README.md Normal file
View 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

View File

@ -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
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.
*/
export {encode, decode} from './lib/thirty-two/thirty-two.js'
export { encode, decode } from './lib/base32/base32.js'

View File

@ -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
of this software and associated documentation files (the "Software"), to deal
@ -37,11 +37,11 @@ var byteTable = [
function quintetCount(buff) {
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) {
if(!Buffer.isBuffer(plain)){
if (!Buffer.isBuffer(plain)) {
plain = new Buffer(plain);
}
var i = 0;
@ -52,10 +52,10 @@ export function encode(plain) {
/* byte by byte isn't as pretty as quintet by quintet but tests a bit
faster. will have to revisit. */
while(i < plain.length) {
while (i < plain.length) {
var current = plain[i];
if(shiftIndex > 3) {
if (shiftIndex > 3) {
digit = current & (0xff >> shiftIndex);
shiftIndex = (shiftIndex + 5) % 8;
digit = (digit << shiftIndex) | ((i + 1 < plain.length) ?
@ -64,14 +64,14 @@ export function encode(plain) {
} else {
digit = (current >> (8 - (shiftIndex + 5))) & 0x1f;
shiftIndex = (shiftIndex + 5) % 8;
if(shiftIndex === 0) i++;
if (shiftIndex === 0) i++;
}
encoded[j] = charTable.charCodeAt(digit);
j++;
}
for(i = j; i < encoded.length; i++) {
for (i = j; i < encoded.length; i++) {
encoded[i] = 0x3d; //'='.charCodeAt(0)
}
@ -83,27 +83,27 @@ export function decode(encoded) {
var plainDigit = 0;
var plainChar;
var plainPos = 0;
if(!Buffer.isBuffer(encoded)){
if (!Buffer.isBuffer(encoded)) {
encoded = new Buffer(encoded);
}
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
faster. will have to revisit. */
for(var i = 0; i < encoded.length; i++) {
if(encoded[i] === 0x3d){ //'='
for (var i = 0; i < encoded.length; i++) {
if (encoded[i] === 0x3d) { //'='
break;
}
var encodedByte = encoded[i] - 0x30;
if(encodedByte < byteTable.length) {
if (encodedByte < byteTable.length) {
plainDigit = byteTable[encodedByte];
if(shiftIndex <= 3) {
if (shiftIndex <= 3) {
shiftIndex = (shiftIndex + 5) % 8;
if(shiftIndex === 0) {
if (shiftIndex === 0) {
plainChar |= plainDigit;
decoded[plainPos] = plainChar;
plainPos++;

View File

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

View File

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

View File

@ -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/

View File

@ -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

View File

@ -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');
});
});

View File

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

View File

@ -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)

View File

@ -1,9 +1,2 @@
/*
* @Author: wuyanxin
* @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'
export { HOTP } from './lib/hotp.js'
export { TOTP } from './lib/totp.js'

View File

@ -1,11 +1,4 @@
/*
* @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 { encode, decode } from '../../base32/index.js'
import jsSHA from 'jssha'
export class HOTP {
@ -56,9 +49,9 @@ export class HOTP {
_truncat(hmac_result) {
const offset = hmac_result[19].charCodeAt() & 0xf;
const bin_code = (hmac_result[offset].charCodeAt() & 0x7f) << 24
| (hmac_result[offset+1].charCodeAt() & 0xff) << 16
| (hmac_result[offset+2].charCodeAt() & 0xff) << 8
| (hmac_result[offset+3].charCodeAt() & 0xff);
| (hmac_result[offset + 1].charCodeAt() & 0xff) << 16
| (hmac_result[offset + 2].charCodeAt() & 0xff) << 8
| (hmac_result[offset + 3].charCodeAt() & 0xff);
let otp = (bin_code % 10 ** this.digit).toString();
while (otp.length < this.digit) {
otp = '0' + otp;

View File

@ -1,14 +1,7 @@
/*
* @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";
import { HOTP } from "./hotp.js";
export class TOTP extends HOTP {
constructor(key){
constructor(key) {
super(key)
}
@ -20,5 +13,4 @@ export class TOTP extends HOTP {
verify(otp, timeStep = 30, t0 = 0) {
return otp === this.genOTP(timeStep, t0);
}
}

View File

@ -1,6 +1,6 @@
{
"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. ",
"main": "index.js",
"scripts": {
@ -24,8 +24,7 @@
},
"homepage": "https://github.com/wuyanxin/totp.js#readme",
"dependencies": {
"jssha": "^2.3.1",
"thirty-two": "^1.0.2"
"jssha": "^2.3.1"
},
"devDependencies": {
"mocha": "^5.0.4"