Socket
Socket
Sign inDemoInstall

c32check

Package Overview
Dependencies
7
Maintainers
1
Versions
12
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 0.0.2 to 0.0.3

tests/unitTests/data/random.json

20

package.json
{
"name": "c32check",
"version": "0.0.2",
"description": "z-base-32 checksum encoding",
"version": "0.0.3",
"description": "Crockford base-32 checksum encoding",
"main": "lib/index",

@@ -12,2 +12,3 @@ "scripts": {

"unit-test": "npm run lint && npm run flow && npm run compile && npm run compile-tests && npm run browserify && node ./tests/unitTests/lib/index.js",
"data-set-test": "npm run lint && npm run flow && npm run compile && npm run compile-tests && npm run browserify && BIG_DATA_TESTS=1 node ./tests/unitTests/lib/index.js",
"build": "npm run flow && npm run compile && npm run browserify",

@@ -69,22 +70,8 @@ "flow": "flow",

"babel-preset-flow": "^6.23.0",
"bluebird": "^3.5.1",
"blue-tape": "^1.0.0",
"browserify": "^13.1.1",
"documentation": "^4.0.0-rc.1",
"eslint": "^2.10.2",
"eslint-config-airbnb": "^9.0.1",
"eslint-plugin-flowtype": "^2.49.3",
"eslint-plugin-import": "^1.8.1",
"eslint-plugin-jsx-a11y": "^1.2.2",
"eslint-plugin-react": "^5.1.1",
"express": "^4.15.0",
"fetch-mock": "^5.5.0",
"flow-bin": "^0.49.1",
"mock-local-storage": "^1.0.5",
"nock": "^9.1.6",
"node-fetch": "^1.6.3",
"nyc": "^11.4.1",
"opn": "^4.0.2",
"proxyquire": "^1.8.0",
"sinon": "^4.2.1",
"tape": "^4.6.3",

@@ -94,3 +81,2 @@ "tape-promise": "^2.0.1"

"dependencies": {
"bigi": "^1.4.2",
"ripemd160": "^2.0.1"

@@ -97,0 +83,0 @@ },

/* @flow */
const bigi = require('bigi')
export const c32 = '0123456789ABCDEFGHJKMNPQRSTVWXYZ'
const hex = '0123456789abcdef'

@@ -23,21 +22,40 @@ /**

}
inputHex = inputHex.toLowerCase()
const res = []
const zero = bigi.fromByteArrayUnsigned('0')
const base = bigi.fromByteArrayUnsigned(`${c32.length}`)
const zeroPrefix = Buffer.from(inputHex, 'hex').toString().match(/^\u0000*/)
const numLeadingZeroBytes = zeroPrefix ? zeroPrefix[0].length : 0
let res = []
let carry = 0
for (let i = inputHex.length - 1; i >= 0; i--) {
if (carry < 4) {
const currentCode = hex.indexOf(inputHex[i]) >> carry
let nextCode = 0
if (i !== 0) {
nextCode = hex.indexOf(inputHex[i - 1])
}
// carry = 0, nextBits is 1, carry = 1, nextBits is 2
const nextBits = 1 + carry
const nextLowBits = (nextCode % (1<<nextBits)) << (5-nextBits)
const curC32Digit = c32[currentCode + nextLowBits]
carry = nextBits
res.unshift(curC32Digit)
} else {
carry = 0
}
}
let val = bigi.fromHex(inputHex)
while (val.compareTo(zero) > 0) {
const divRem = val.divideAndRemainder(base)
const rem = divRem[1].toByteArray()[0] // between 0 and c32.length - 1
res.unshift(c32[rem])
val = divRem[0]
let C32leadingZeros = 0
for (let i = 0; i < res.length; i++) {
if (res[i] !== '0') {
break
} else {
C32leadingZeros++
}
}
for (let i = 0; i < numLeadingZeroBytes; i++) {
res = res.slice(C32leadingZeros)
const zeroPrefix = Buffer.from(inputHex, 'hex').toString().match(/^\u0000*/)
const numLeadingZeroBytesInHex = zeroPrefix ? zeroPrefix[0].length : 0
for (let i = 0; i < numLeadingZeroBytesInHex; i++) {
res.unshift(c32[0])

@@ -86,15 +104,43 @@ }

const base = bigi.fromByteArrayUnsigned(`${c32.length}`)
const zeroPrefix = c32input.match(`^${c32[0]}*`)
const numLeadingZeroBytes = zeroPrefix ? zeroPrefix[0].length : 0
let res = bigi.fromByteArrayUnsigned('0')
for (let i = 0; i < c32input.length; i++) {
res = res.multiply(base)
res = res.add(bigi.fromByteArrayUnsigned(`${c32.indexOf(c32input[i])}`))
let res = []
let carry = 0
let carryBits = 0
for (let i = c32input.length - 1; i >= 0; i--) {
if (carryBits === 4) {
res.unshift(hex[carry])
carryBits = 0
carry = 0
}
const currentCode = c32.indexOf(c32input[i]) << carryBits
const currentValue = currentCode + carry
const currentHexDigit = hex[currentValue % 16]
carryBits += 1
carry = currentValue >> 4
if (carry > 1 << carryBits) {
throw new Error('Panic error in decoding.')
}
res.unshift(currentHexDigit)
}
// one last carry
res.unshift(hex[carry])
let hexStr = res.toHex()
if (res.length % 2 === 1) {
res.unshift('0')
}
let hexLeadingZeros = 0
for (let i = 0; i < res.length; i++) {
if (res[i] !== '0') {
break
} else {
hexLeadingZeros++
}
}
res = res.slice(hexLeadingZeros - (hexLeadingZeros % 2))
let hexStr = res.join('')
for (let i = 0; i < numLeadingZeroBytes; i++) {

@@ -110,4 +156,4 @@ hexStr = `00${hexStr}`

}
return hexStr
}
import test from 'tape-promise/tape'
import process from 'process'
import {

@@ -166,2 +166,32 @@ c32encode,

export function c32encodingRandomBytes() {
const testData = require('../data/random.json')
test('c32encode', (t) => {
t.plan(testData.length)
testData.map((testData) => {
const actualC32 = c32encode(testData.hex, testData.c32.length)
const expectedC32 = testData.c32
if (actualC32.length === expectedC32.length + 1) {
t.equal(actualC32, `0${expectedC32}`, 'Should match test data from external library.')
} else {
t.equal(actualC32, expectedC32, 'Should match test data from external library.')
}
})
})
test('c32decode', (t) => {
t.plan(testData.length)
testData.map((testData) => {
const actualHex = c32decode(testData.c32, testData.hex.length / 2)
const expectedHex = testData.hex
t.equal(actualHex, expectedHex, 'Should match test hex data from external library.')
if (actualHex !== expectedHex) {
throw new Error('FAILING FAST HERE')
}
})
})
}
export function c32checkEncodingTests() {

@@ -551,4 +581,8 @@ const hexStrings = [

c32encodingTests()
c32checkEncodingTests()
c32addressTests()
if (process.env.BIG_DATA_TESTS) {
c32encodingRandomBytes()
} else {
c32encodingTests()
c32checkEncodingTests()
c32addressTests()
}

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc