branca-js interop between v0.3.0 and v0.4.0

node v14.20.1
version: master
endpointsharetweet
var assert = require('assert') const PAYLOAD = "test" const keyString = "supersecretkeyyoushouldnotcommit"; console.log("length keyString", keyString.length) const keyStringToHex = Buffer.from(keyString, 'utf8').toString('hex') // console.log("keyStringToHex", keyStringToHex) console.log("keyStringToHex.length", keyStringToHex.length) const keyStringToBuff = Buffer.from(keyStringToHex) // console.log("keyStringToBuff", keyStringToBuff) console.log("keyStringToBuff.length", keyStringToBuff.length) const keyTestVector = "73757065727365637265746b6579796f7573686f756c646e6f74636f6d6d6974"; // console.log("keyTestVector", keyTestVector) console.log("keyTestVector.length", keyTestVector.length) assert(keyStringToHex, keyTestVector) console.log("keys are equivalent")
Branca 0.3.0 with keyString
const branca3 = require("branca@0.3.0")(keyString); const t3 = branca3.encode(PAYLOAD); console.log(t3); const t3Decoded = branca3.decode(t3) assert(t3Decoded.toString() === PAYLOAD) console.log("OK")
FIXME : Branca 0.4.0 does not accept `keyString` (to be expected as new version rejects non hex/Buffer key). A developer who just upgraded from `0.3.0` to `0.4.0` will hit a wall unless they know what the problem is and how to fix it, which is not currently documented.
try { const branca4 = require("branca@0.4.0")(keyString); } catch(err) { console.log(err.message) }
Branca 0.4.0 does accept `keyStringToHex` if the developer knows to convert original key with `Buffer.from(keyString, 'utf8').toString('hex')`
const branca4 = require("branca@0.4.0")(keyStringToHex); const t4 = branca4.encode(PAYLOAD); console.log(t4); const t4Decoded = branca4.decode(t4) assert(t4Decoded.toString() === PAYLOAD) console.log("OK")
Decode branca3 tokens with branca4
const t3DecodedByBranca4 = branca4.decode(t3) assert(t3DecodedByBranca4.toString() === PAYLOAD) console.log("OK")
Decode branca4 tokens with branca3
const t4DecodedByBranca3 = branca3.decode(t4) assert(t4DecodedByBranca3.toString() === PAYLOAD) console.log("OK")
Loading…

no comments

    sign in to comment