Beta 7 with some repo names changed.

This commit is contained in:
Luis Fernando Molina committed 2020-11-15 13:05:19 +01:00
1 parent 0f7b40cca7
commit 963c19493a
3408 files changed
+462217 -15516

No files matched your search

+26
View File
@@ -0,0 +1,26 @@
'use strict'
class Base {
constructor (name, code, implementation, alphabet) {
this.name = name
this.code = code
this.alphabet = alphabet
if (implementation && alphabet) {
this.engine = implementation(alphabet)
}
}
encode (stringOrBuffer) {
return this.engine.encode(stringOrBuffer)
}
decode (stringOrBuffer) {
return this.engine.decode(stringOrBuffer)
}
isImplemented () {
return this.engine
}
}
module.exports = Base
+21
View File
@@ -0,0 +1,21 @@
'use strict'
const { Buffer } = require('buffer')
module.exports = function base16 (alphabet) {
return {
encode (input) {
if (typeof input === 'string') {
return Buffer.from(input).toString('hex')
}
return input.toString('hex')
},
decode (input) {
for (const char of input) {
if (alphabet.indexOf(char) < 0) {
throw new Error('invalid base16 character')
}
}
return Buffer.from(input, 'hex')
}
}
}
+81
View File
@@ -0,0 +1,81 @@
'use strict'
function decode (input, alphabet) {
input = input.replace(new RegExp('=', 'g'), '')
const length = input.length
let bits = 0
let value = 0
let index = 0
const output = new Uint8Array((length * 5 / 8) | 0)
for (let i = 0; i < length; i++) {
value = (value << 5) | alphabet.indexOf(input[i])
bits += 5
if (bits >= 8) {
output[index++] = (value >>> (bits - 8)) & 255
bits -= 8
}
}
return output.buffer
}
function encode (buffer, alphabet) {
const length = buffer.byteLength
const view = new Uint8Array(buffer)
const padding = alphabet.indexOf('=') === alphabet.length - 1
if (padding) {
alphabet = alphabet.substring(0, alphabet.length - 1)
}
let bits = 0
let value = 0
let output = ''
for (let i = 0; i < length; i++) {
value = (value << 8) | view[i]
bits += 8
while (bits >= 5) {
output += alphabet[(value >>> (bits - 5)) & 31]
bits -= 5
}
}
if (bits > 0) {
output += alphabet[(value << (5 - bits)) & 31]
}
if (padding) {
while ((output.length % 8) !== 0) {
output += '='
}
}
return output
}
module.exports = function base32 (alphabet) {
return {
encode (input) {
if (typeof input === 'string') {
return encode(Uint8Array.from(input), alphabet)
}
return encode(input, alphabet)
},
decode (input) {
for (const char of input) {
if (alphabet.indexOf(char) < 0) {
throw new Error('invalid base32 character')
}
}
return decode(input, alphabet)
}
}
}
+44
View File
@@ -0,0 +1,44 @@
'use strict'
const { Buffer } = require('buffer')
module.exports = function base64 (alphabet) {
// The alphabet is only used to know:
// 1. If padding is enabled (must contain '=')
// 2. If the output must be url-safe (must contain '-' and '_')
// 3. If the input of the output function is valid
// The alphabets from RFC 4648 are always used.
const padding = alphabet.indexOf('=') > -1
const url = alphabet.indexOf('-') > -1 && alphabet.indexOf('_') > -1
return {
encode (input) {
let output = ''
if (typeof input === 'string') {
output = Buffer.from(input).toString('base64')
} else {
output = input.toString('base64')
}
if (url) {
output = output.replace(/\+/g, '-').replace(/\//g, '_')
}
const pad = output.indexOf('=')
if (pad > 0 && !padding) {
output = output.substring(0, pad)
}
return output
},
decode (input) {
for (const char of input) {
if (alphabet.indexOf(char) < 0) {
throw new Error('invalid base64 character')
}
}
return Buffer.from(input, 'base64')
}
}
}
+42
View File
@@ -0,0 +1,42 @@
'use strict'
const Base = require('./base.js')
const baseX = require('base-x')
const base16 = require('./base16')
const base32 = require('./base32')
const base64 = require('./base64')
// name, code, implementation, alphabet
const constants = [
['base1', '1', '', '1'],
['base2', '0', baseX, '01'],
['base8', '7', baseX, '01234567'],
['base10', '9', baseX, '0123456789'],
['base16', 'f', base16, '0123456789abcdef'],
['base32', 'b', base32, 'abcdefghijklmnopqrstuvwxyz234567'],
['base32pad', 'c', base32, 'abcdefghijklmnopqrstuvwxyz234567='],
['base32hex', 'v', base32, '0123456789abcdefghijklmnopqrstuv'],
['base32hexpad', 't', base32, '0123456789abcdefghijklmnopqrstuv='],
['base32z', 'h', base32, 'ybndrfg8ejkmcpqxot1uwisza345h769'],
['base58flickr', 'Z', baseX, '123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ'],
['base58btc', 'z', baseX, '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'],
['base64', 'm', base64, 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'],
['base64pad', 'M', base64, 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/='],
['base64url', 'u', base64, 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_'],
['base64urlpad', 'U', base64, 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_=']
]
const names = constants.reduce((prev, tupple) => {
prev[tupple[0]] = new Base(tupple[0], tupple[1], tupple[2], tupple[3])
return prev
}, {})
const codes = constants.reduce((prev, tupple) => {
prev[tupple[1]] = names[tupple[0]]
return prev
}, {})
module.exports = {
names: names,
codes: codes
}
+132
View File
@@ -0,0 +1,132 @@
/**
* Implementation of the [multibase](https://github.com/multiformats/multibase) specification.
* @module Multibase
*/
'use strict'
const { Buffer } = require('buffer')
const constants = require('./constants')
exports = module.exports = multibase
exports.encode = encode
exports.decode = decode
exports.isEncoded = isEncoded
exports.names = Object.freeze(Object.keys(constants.names))
exports.codes = Object.freeze(Object.keys(constants.codes))
const errNotSupported = new Error('Unsupported encoding')
/**
* Create a new buffer with the multibase varint+code.
*
* @param {string|number} nameOrCode - The multibase name or code number.
* @param {Buffer} buf - The data to be prefixed with multibase.
* @memberof Multibase
* @returns {Buffer}
*/
function multibase (nameOrCode, buf) {
if (!buf) {
throw new Error('requires an encoded buffer')
}
const base = getBase(nameOrCode)
const codeBuf = Buffer.from(base.code)
const name = base.name
validEncode(name, buf)
return Buffer.concat([codeBuf, buf])
}
/**
* Encode data with the specified base and add the multibase prefix.
*
* @param {string|number} nameOrCode - The multibase name or code number.
* @param {Buffer} buf - The data to be encoded.
* @returns {Buffer}
* @memberof Multibase
*/
function encode (nameOrCode, buf) {
const base = getBase(nameOrCode)
const name = base.name
return multibase(name, Buffer.from(base.encode(buf)))
}
/**
* Takes a buffer or string encoded with multibase header, decodes it and
* returns the decoded buffer
*
* @param {Buffer|string} bufOrString
* @returns {Buffer}
* @memberof Multibase
*
*/
function decode (bufOrString) {
if (Buffer.isBuffer(bufOrString)) {
bufOrString = bufOrString.toString()
}
const code = bufOrString.substring(0, 1)
bufOrString = bufOrString.substring(1, bufOrString.length)
if (typeof bufOrString === 'string') {
bufOrString = Buffer.from(bufOrString)
}
const base = getBase(code)
return Buffer.from(base.decode(bufOrString.toString()))
}
/**
* Is the given data multibase encoded?
*
* @param {Buffer|string} bufOrString
* @returns {boolean}
* @memberof Multibase
*/
function isEncoded (bufOrString) {
if (Buffer.isBuffer(bufOrString)) {
bufOrString = bufOrString.toString()
}
// Ensure bufOrString is a string
if (Object.prototype.toString.call(bufOrString) !== '[object String]') {
return false
}
const code = bufOrString.substring(0, 1)
try {
const base = getBase(code)
return base.name
} catch (err) {
return false
}
}
/**
* @param {string} name
* @param {Buffer} buf
* @private
* @returns {undefined}
*/
function validEncode (name, buf) {
const base = getBase(name)
base.decode(buf.toString())
}
function getBase (nameOrCode) {
let base
if (constants.names[nameOrCode]) {
base = constants.names[nameOrCode]
} else if (constants.codes[nameOrCode]) {
base = constants.codes[nameOrCode]
} else {
throw errNotSupported
}
if (!base.isImplemented()) {
throw new Error('Base ' + nameOrCode + ' is not implemented yet')
}
return base
}