Skip to content

Commit

Permalink
token shared lib: Implement missing functions (#5)
Browse files Browse the repository at this point in the history
* token shared lib: Implement missing functions

Implement missing features in shared library needed to get
encoder/decoder to function properly. Also fix the accuracy issue with
siphash lib by using hex values.

* fix lint errors

* replace siphash source with dep
  • Loading branch information
beesaferoot authored May 29, 2024
1 parent fb8b2e8 commit eaae921
Show file tree
Hide file tree
Showing 11 changed files with 191 additions and 103 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ npm-debug.log*
yarn-debug.log*
yarn-error.log*

.vscode
24 changes: 12 additions & 12 deletions eslint.config.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import js from "@eslint/js";
const js = require('@eslint/js')

export default [
module.exports = [
js.configs.recommended,

{
rules: {
"no-unused-vars": "off",
"no-undef": "off"
}
},
{
ignores: ["test/*", "lib/*"]
}
];
{
rules: {
'no-unused-vars': 'off',
'no-undef': 'off',
},
},
{
ignores: ['test/*', 'lib/*'],
},
]
1 change: 0 additions & 1 deletion lib/siphash.js

This file was deleted.

32 changes: 19 additions & 13 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@
"node": ">=18.19.0"
},
"dependencies": {
"bigint-conversion": "^2.4.3",
"buffer": "^6.0.3",
"siphash24": "^1.3.1"
"siphash": "^1.1.0"
}
}
45 changes: 22 additions & 23 deletions src/decoder.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ const TokenTypes = require('./constants').TokenTypes
const shared = require('./token').OpenPAYGOTokenShared

class OpenPAYGOTokenDecoder {
MAX_TOKEN_JUMP = 64
MAX_TOKEN_JUMP_COUNTER_SYNC = 100
MAX_UNUSED_OLDER_TOKENS = 8 * 2
static MAX_TOKEN_JUMP = 64
static MAX_TOKEN_JUMP_COUNTER_SYNC = 100
static MAX_UNUSED_OLDER_TOKENS = 8 * 2

static decodeToken({
token = '',
Expand Down Expand Up @@ -66,11 +66,11 @@ class OpenPAYGOTokenDecoder {
// })
// }

var { value, tokenType, count, updatedCounts } =
var { value, tokenType, newCount, updatedCounts } =
this.getActivationValueCountAndTypefromToken({
token,
startingCode,
secretKeyHex,
keyHex: secretKeyHex,
count,
restrictedDigitSet,
usedCounts,
Expand All @@ -83,7 +83,7 @@ class OpenPAYGOTokenDecoder {
return {
value: value,
tokenType: tokenType,
count: count,
count: newCount,
updatedCounts: updatedCounts,
}
}
Expand All @@ -99,29 +99,29 @@ class OpenPAYGOTokenDecoder {
if (restrictedDigitSet) {
token = shared.convertFrom4digitToken(token)
}
const validOldToken = false
let validOldToken = false
// obtain base of token
const tokenBase = shared.getTokenBase(token)
// put base into the starting code
const currentCode = shared.putBaseInToken(startingCode, tokenBase)
let currentCode = shared.putBaseInToken(startingCode, tokenBase)
// obtain base of starting code
const startCodeBase = shared.getTokenBase(startingCode)

let value = this.decodeBase(startCodeBase, tokenBase)
let maxCountTry
if (value === TokenTypes.COUNTER_SYNC) {
maxCountTry = count + this.prototype.MAX_TOKEN_JUMP_COUNTER_SYNC + 1
maxCountTry = count + this.MAX_TOKEN_JUMP_COUNTER_SYNC + 1
} else {
maxCountTry = count + this.prototype.MAX_TOKEN_JUMP + 1
maxCountTry = count + this.MAX_TOKEN_JUMP + 1
}

for (let cnt = 0; cnt <= maxCountTry; cnt++) {
for (let cnt = 0; cnt < maxCountTry; cnt++) {
const maskedToken = shared.putBaseInToken(currentCode, tokenBase)
let tokenType
if (cnt % 2 !== 0) {
if (value === shared.prototype.COUNTER_SYNC_VALUE) {
if (value === shared.COUNTER_SYNC_VALUE) {
tokenType = TokenTypes.COUNTER_SYNC
} else if (value === shared.prototype.PAYG_DISABLE_VALUE) {
} else if (value === shared.PAYG_DISABLE_VALUE) {
tokenType = TokenTypes.PAYG_DISABLE_VALUE
} else {
tokenType = TokenTypes.SET_TIME
Expand All @@ -147,7 +147,7 @@ class OpenPAYGOTokenDecoder {
updatedCounts,
}
} else {
validOldToken = True
validOldToken = true
}
}
currentCode = shared.genNextToken(currentCode, keyHex)
Expand All @@ -169,14 +169,14 @@ class OpenPAYGOTokenDecoder {
}

static countIsValid(count, lastCount, value, type, usedCounts) {
if (value === shared.prototype.COUNTER_SYNC_VALUE) {
if (count > lastCount - this.prototype.MAX_TOKEN_JUMP) {
if (value === shared.COUNTER_SYNC_VALUE) {
if (count > lastCount - this.MAX_TOKEN_JUMP) {
return true
}
} else if (count > lastCount) {
return true
} else if (this.prototype.MAX_UNUSED_OLDER_TOKENS > 0) {
if (count > lastCount - this.prototype.MAX_UNUSED_OLDER_TOKENS) {
} else if (this.MAX_UNUSED_OLDER_TOKENS > 0) {
if (count > lastCount - this.MAX_UNUSED_OLDER_TOKENS) {
if (
usedCounts.includes(count) &&
type === TokenTypes.ADD_TIME
Expand All @@ -185,7 +185,7 @@ class OpenPAYGOTokenDecoder {
}
}
}
return False
return false
}

static updateUsedCounts(pastUsedCounts, value, newCount, type) {
Expand All @@ -194,14 +194,13 @@ class OpenPAYGOTokenDecoder {
}
let highestCount = pastUsedCounts.length ? Math.max(pastUsedCounts) : 0
highestCount = newCount > highestCount ? newCount : highestCount
const bottomRange =
highestCount - this.prototype.MAX_UNUSED_OLDER_TOKENS
const bottomRange = highestCount - this.MAX_UNUSED_OLDER_TOKENS
const usedCounts = []

if (
type !== TokenTypes.ADD_TIME ||
value === shared.prototype.COUNTER_SYNC_VALUE ||
value === shared.prototype.PAYG_DISABLE_VALUE
value === shared.COUNTER_SYNC_VALUE ||
value === shared.PAYG_DISABLE_VALUE
) {
// If it is not an Add-Time token, we mark all the past tokens as used in the
// range
Expand Down
10 changes: 5 additions & 5 deletions src/encoder.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,16 @@ class OpenPAYGOTokenEncoder {
throw new Error("'value' argument is undefined.")
}
value = Math.round(value * valueDivider)
if (value > shared.prototype.MAX_ACTIVATION_VALUE) {
if (value > shared.MAX_ACTIVATION_VALUE) {
throw new Error('The value provided is too high.')
}
} else if (value !== undefined) {
throw new Error('A value is not allowed for this token type.')
} else {
if (tokenType === TokenTypes.DISABLE_PAYG) {
value = shared.prototype.PAYG_DISABLE_VALUE
value = shared.PAYG_DISABLE_VALUE
} else if (tokenType === TokenTypes.COUNTER_SYNC) {
value = shared.prototype.COUNTER_SYNC_VALUE
value = shared.COUNTER_SYNC_VALUE
} else {
throw new Error('The token type provided is not supported.')
}
Expand Down Expand Up @@ -67,13 +67,13 @@ class OpenPAYGOTokenEncoder {
let currentToken = shared.putBaseInToken(startingCode, tokenBase)
const newCount = this.getNewCount(count, mode)

for (let i = 0; i < newCount; i++) {
for (let i = 0; i < newCount - 1; i++) {
currentToken = shared.genNextToken(currentToken, key)
}
let finalToken = shared.putBaseInToken(currentToken, tokenBase)

if (restrictDigitSet) {
finalToken = shared.convertTo4dToken(finalToken)
finalToken = shared.convertToNdigitToken(finalToken)
finalToken = String(finalToken).padStart(15, '0')
} else {
finalToken = String(finalToken).padStart(9, '0')
Expand Down
Loading

0 comments on commit eaae921

Please sign in to comment.