Skip to content

Commit

Permalink
v0.1.17: fix base64url and README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
taichunmin committed May 16, 2023
1 parent 79a423e commit 854651e
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 8 deletions.
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ import { Pn532, Packet, utils: Pn532utils } from 'pn532.js'
import Crypto1 from 'pn532.js/Crypto1.js'
import LoggerRxTx from 'pn532.js/plugin/LoggerRxTx.js'
import Pn532Hf14a from 'pn532.js/plugin/Hf14a.js'
import Pn532SerialPortAdapter from 'pn532.js/plugin/SerialPortAdapter.js'
import Pn532WebbleAdapter from 'pn532.js/plugin/WebbleAdapter.js'
import Pn532WebserialAdapter from 'pn532.js/plugin/WebserialAdapter.js'

Expand All @@ -82,6 +83,7 @@ const { Pn532, Packet, utils: Pn532utils } = require('pn532.js')
const Crypto1 = require('pn532.js/Crypto1.js')
const LoggerRxTx = require('pn532.js/plugin/LoggerRxTx.js')
const Pn532Hf14a = require('pn532.js/plugin/Hf14a.js')
const Pn532SerialPortAdapter = require('pn532.js/plugin/SerialPortAdapter.js')
const Pn532WebbleAdapter = require('pn532.js/plugin/WebbleAdapter.js')
const Pn532WebserialAdapter = require('pn532.js/plugin/WebserialAdapter.js')
```
Expand Down Expand Up @@ -153,8 +155,9 @@ console.log(JSON.stringify(await pn532ble.getFirmwareVersion())) // {"firmware":
// Pn532SerialPortAdapter
import Pn532SerialPortAdapter from 'pn532.js/plugin/SerialPortAdapter.js'
// Run serialport-list to list port, see https://serialport.io/docs/bin-list
pn532.use(new Pn532SerialPortAdapter(), { path: '/dev/tty.usbserial-120' })
console.log(JSON.stringify(await pn532ble.getFirmwareVersion())) // {"firmware":"1.6","ic":"PN532","iso14443a":true,"iso14443b":true,"iso18092":true}
const pn532node = new Pn532()
pn532node.use(new Pn532SerialPortAdapter(), { path: '/dev/tty.usbserial-120' })
console.log(JSON.stringify(await pn532node.getFirmwareVersion())) // {"firmware":"1.6","ic":"PN532","iso14443a":true,"iso14443b":true,"iso18092":true}
```

### Read UID, ATQA, SAK from Mifare Classic 1k
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"name": "pn532.js",
"type": "module",
"unpkg": "dist/pn532.min.js",
"version": "0.1.16",
"version": "0.1.17",
"bugs": {
"url": "https://github.com/taichunmin/pn532.js/issues"
},
Expand Down
40 changes: 35 additions & 5 deletions src/Packet.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,15 @@
*/
import _ from 'lodash'

const BASE64_CHAR = _.transform('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/', (m, v, k) => {
k = _.toInteger(k)
m.set(k, v).set(v, k)
}, new Map()).set('-', 62).set('_', 63)

const BASE64URL_CHAR = _.transform('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_', (m, v, k) => {
k = _.toInteger(k)
m.set(k, v).set(v, k)
}, new Map()).set('+', 62).set(62, '+').set('/', 63).set(63, '/')
}, new Map()).set('+', 62).set('/', 63)

/**
* The Packet class extends Uint8Array, contains some member function of DataView and add some helper function.
Expand Down Expand Up @@ -83,10 +88,10 @@ export default class Packet extends Uint8Array {
const pack = new Packet(base64.length * 3 >>> 2)
let parsedLen = 0
for (let i = 0; i < base64.length; i++) {
const u24 = (BASE64URL_CHAR.get(base64[i]) << 18) +
(BASE64URL_CHAR.get(base64[i + 1]) << 12) +
(BASE64URL_CHAR.get(base64[i + 2]) << 6) +
BASE64URL_CHAR.get(base64[i + 3])
const u24 = (BASE64_CHAR.get(base64[i]) << 18) +
(BASE64_CHAR.get(base64[i + 1]) << 12) +
(BASE64_CHAR.get(base64[i + 2]) << 6) +
BASE64_CHAR.get(base64[i + 3])
pack[parsedLen++] = (u24 >>> 16) & 0xFF
pack[parsedLen++] = (u24 >>> 8) & 0xFF
pack[parsedLen++] = (u24 >>> 0) & 0xFF
Expand Down Expand Up @@ -211,6 +216,31 @@ export default class Packet extends Uint8Array {
*/
get utf8 () { return new TextDecoder().decode(this) }

/**
* base64 string of the Packet
* @example
* console.log(Packet.fromHex('616263').base64)
* // YWJj
* @member {string}
*/
get base64 () {
const tmp1 = []
for (let i = 0; i < this.length; i += 3) {
const u24 = (this[i] << 16) +
((i + 1 < this.length ? this[i + 1] : 0) << 8) +
(i + 2 < this.length ? this[i + 2] : 0)
tmp1.push(...[
BASE64_CHAR.get(u24 >>> 18 & 0x3F),
BASE64_CHAR.get(u24 >>> 12 & 0x3F),
BASE64_CHAR.get(u24 >>> 6 & 0x3F),
BASE64_CHAR.get(u24 >>> 0 & 0x3F),
])
}
const tmp2 = tmp1.length + (this.length + 2) % 3 - 2
for (let i = tmp2; i < tmp1.length; i++) tmp1[i] = '='
return tmp1.join('')
}

/**
* base64url string of the Packet
* @example
Expand Down
13 changes: 13 additions & 0 deletions src/Packet.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,8 @@ test.each([
})

test.each([
['-_-_', '-_-_'],
['+/+/', '-_-_'],
['MQ', 'MQ'],
['MTI', 'MTI'],
['MTIz', 'MTIz'],
Expand All @@ -231,6 +233,17 @@ test.each([
expect(actual).toEqual(expected)
})

test.each([
['-_-_', '+/+/'],
['+/+/', '+/+/'],
['MQ', 'MQ=='],
['MTI', 'MTI='],
['MTIz', 'MTIz'],
])('Packet.fromBase64(%j).base64 = %j', async (hex, expected) => {
const actual = Packet.fromBase64(hex).base64
expect(actual).toEqual(expected)
})

test.each([
['12', 8],
['1234', 16],
Expand Down

0 comments on commit 854651e

Please sign in to comment.