-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
25 lines (22 loc) · 953 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
const table = 'fZodR9XQDSUm21yCkr6zBqiveYah8bt4xsWpHnJE7jL5VG3guMTKNPAwcF'
const tr = Array.from(table).reduce((o, c, i) => Object.assign(o, { [c]: i }), { })
const s = [11, 10, 3, 8, 4, 6]
const xor = 177451812
const add = 8728348608
/**
* Convert a BV string to AV number
* @param {string} bv The BV string to be converted to AV number
* @returns {number} The AV number converted from the provided BV string
*/
function BVtoAV (bv) {
return (new Uint8Array(6).reduce((r, _, i) => r + tr[bv[s[i]]] * 58 ** i, 0) - add) ^ xor
}
/**
* Convert a AV number to BV string
* @param {number} av The AV number to be converted to BV string
* @returns {string} The BV string converted from the provided AV number
*/
function AVtoBV (av) {
return (new Uint8Array(6).reduce((r, _, i) => { r.splice(s[i], 1, table[Math.floor(((av ^ xor) + add) / 58 ** i % 58)]); return r }, Array.from('BV1 4 1 7 '))).join('')
}
module.exports = { BVtoAV, AVtoBV }