128 lines
4.9 KiB
JavaScript
128 lines
4.9 KiB
JavaScript
"use strict";
|
|
/*
|
|
* Copyright 2018-2020 The NATS Authors
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
* you may not use this file except in compliance with the License.
|
|
* You may obtain a copy of the License at
|
|
*
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
*
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
* See the License for the specific language governing permissions and
|
|
* limitations under the License.
|
|
*/
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.Codec = void 0;
|
|
const crc16_1 = require("./crc16");
|
|
const nkeys_1 = require("./nkeys");
|
|
const base32_1 = require("./base32");
|
|
/**
|
|
* @ignore
|
|
*/
|
|
class Codec {
|
|
static encode(prefix, src) {
|
|
if (!src || !(src instanceof Uint8Array)) {
|
|
throw new nkeys_1.NKeysError(nkeys_1.NKeysErrorCode.SerializationError);
|
|
}
|
|
if (!nkeys_1.Prefixes.isValidPrefix(prefix)) {
|
|
throw new nkeys_1.NKeysError(nkeys_1.NKeysErrorCode.InvalidPrefixByte);
|
|
}
|
|
return Codec._encode(false, prefix, src);
|
|
}
|
|
static encodeSeed(role, src) {
|
|
if (!src) {
|
|
throw new nkeys_1.NKeysError(nkeys_1.NKeysErrorCode.ApiError);
|
|
}
|
|
if (!nkeys_1.Prefixes.isValidPublicPrefix(role)) {
|
|
throw new nkeys_1.NKeysError(nkeys_1.NKeysErrorCode.InvalidPrefixByte);
|
|
}
|
|
if (src.byteLength !== 32) {
|
|
throw new nkeys_1.NKeysError(nkeys_1.NKeysErrorCode.InvalidSeedLen);
|
|
}
|
|
return Codec._encode(true, role, src);
|
|
}
|
|
static decode(expected, src) {
|
|
if (!nkeys_1.Prefixes.isValidPrefix(expected)) {
|
|
throw new nkeys_1.NKeysError(nkeys_1.NKeysErrorCode.InvalidPrefixByte);
|
|
}
|
|
const raw = Codec._decode(src);
|
|
if (raw[0] !== expected) {
|
|
throw new nkeys_1.NKeysError(nkeys_1.NKeysErrorCode.InvalidPrefixByte);
|
|
}
|
|
return raw.slice(1);
|
|
}
|
|
static decodeSeed(src) {
|
|
const raw = Codec._decode(src);
|
|
const prefix = Codec._decodePrefix(raw);
|
|
if (prefix[0] != nkeys_1.Prefix.Seed) {
|
|
throw new nkeys_1.NKeysError(nkeys_1.NKeysErrorCode.InvalidSeed);
|
|
}
|
|
if (!nkeys_1.Prefixes.isValidPublicPrefix(prefix[1])) {
|
|
throw new nkeys_1.NKeysError(nkeys_1.NKeysErrorCode.InvalidPrefixByte);
|
|
}
|
|
return ({ buf: raw.slice(2), prefix: prefix[1] });
|
|
}
|
|
// unsafe encode no prefix/role validation
|
|
static _encode(seed, role, payload) {
|
|
// offsets for this token
|
|
const payloadOffset = seed ? 2 : 1;
|
|
const payloadLen = payload.byteLength;
|
|
const checkLen = 2;
|
|
const cap = payloadOffset + payloadLen + checkLen;
|
|
const checkOffset = payloadOffset + payloadLen;
|
|
const raw = new Uint8Array(cap);
|
|
// make the prefixes human readable when encoded
|
|
if (seed) {
|
|
const encodedPrefix = Codec._encodePrefix(nkeys_1.Prefix.Seed, role);
|
|
raw.set(encodedPrefix);
|
|
}
|
|
else {
|
|
raw[0] = role;
|
|
}
|
|
raw.set(payload, payloadOffset);
|
|
//calculate the checksum write it LE
|
|
const checksum = crc16_1.crc16.checksum(raw.slice(0, checkOffset));
|
|
const dv = new DataView(raw.buffer);
|
|
dv.setUint16(checkOffset, checksum, true);
|
|
return base32_1.base32.encode(raw);
|
|
}
|
|
// unsafe decode - no prefix/role validation
|
|
static _decode(src) {
|
|
if (src.byteLength < 4) {
|
|
throw new nkeys_1.NKeysError(nkeys_1.NKeysErrorCode.InvalidEncoding);
|
|
}
|
|
let raw;
|
|
try {
|
|
raw = base32_1.base32.decode(src);
|
|
}
|
|
catch (ex) {
|
|
throw new nkeys_1.NKeysError(nkeys_1.NKeysErrorCode.InvalidEncoding, ex);
|
|
}
|
|
const checkOffset = raw.byteLength - 2;
|
|
const dv = new DataView(raw.buffer);
|
|
const checksum = dv.getUint16(checkOffset, true);
|
|
const payload = raw.slice(0, checkOffset);
|
|
if (!crc16_1.crc16.validate(payload, checksum)) {
|
|
throw new nkeys_1.NKeysError(nkeys_1.NKeysErrorCode.InvalidChecksum);
|
|
}
|
|
return payload;
|
|
}
|
|
static _encodePrefix(kind, role) {
|
|
// In order to make this human printable for both bytes, we need to do a little
|
|
// bit manipulation to setup for base32 encoding which takes 5 bits at a time.
|
|
const b1 = kind | (role >> 5);
|
|
const b2 = (role & 31) << 3; // 31 = 00011111
|
|
return new Uint8Array([b1, b2]);
|
|
}
|
|
static _decodePrefix(raw) {
|
|
// Need to do the reverse from the printable representation to
|
|
// get back to internal representation.
|
|
const b1 = raw[0] & 248; // 248 = 11111000
|
|
const b2 = (raw[0] & 7) << 5 | ((raw[1] & 248) >> 3); // 7 = 00000111
|
|
return new Uint8Array([b1, b2]);
|
|
}
|
|
}
|
|
exports.Codec = Codec;
|