diff --git a/CHANGELOG.md b/CHANGELOG.md index 125390ec4..1001ef417 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ ## 2.18.0-beta.x +- Add `base64{Decode, Encode, Validate}` as crypto utils +- Extract `base58Validate` from base58 decode checks - Add `isAscii` to allow detection of printable ASCII sequences (including tab, newline) - Add `isUtf8` to allow detection of valid Utf8 sequences diff --git a/packages/util-crypto/src/base58/decode.ts b/packages/util-crypto/src/base58/decode.ts index d862df72f..a8b9d538f 100644 --- a/packages/util-crypto/src/base58/decode.ts +++ b/packages/util-crypto/src/base58/decode.ts @@ -4,7 +4,8 @@ import { bufferToU8a } from '@polkadot/util'; -import { BASE58_ALPHABET, bs58 } from './bs58'; +import { bs58 } from './bs58'; +import validate from './validate'; /** * @name base58Decode @@ -13,11 +14,7 @@ import { BASE58_ALPHABET, bs58 } from './bs58'; * From the provided input, decode the base58 and return the result as an `Uint8Array`. */ export default function base58Decode (value: string): Uint8Array { - for (let i = 0; i < value.length; i++) { - if (!BASE58_ALPHABET.includes(value[i])) { - throw new Error(`Invalid base58 character "${value[i]}" (0x${value.charCodeAt(i).toString(16)}) at index ${i}`); - } - } + validate(value); return bufferToU8a(bs58.decode(value)); } diff --git a/packages/util-crypto/src/base58/index.ts b/packages/util-crypto/src/base58/index.ts index c7104a5dc..e4d23020e 100644 --- a/packages/util-crypto/src/base58/index.ts +++ b/packages/util-crypto/src/base58/index.ts @@ -8,3 +8,4 @@ export { default as base58Decode } from './decode'; export { default as base58Encode } from './encode'; +export { default as base58Validate } from './validate'; diff --git a/packages/util-crypto/src/base58/validate.ts b/packages/util-crypto/src/base58/validate.ts new file mode 100644 index 000000000..a88f1046f --- /dev/null +++ b/packages/util-crypto/src/base58/validate.ts @@ -0,0 +1,21 @@ +// Copyright 2017-2020 @polkadot/util-crypto authors & contributors +// This software may be modified and distributed under the terms +// of the Apache-2.0 license. See the LICENSE file for details. + +import { BASE58_ALPHABET } from './bs58'; + +/** + * @name base58Validate + * @summary Validates a base58 value. + * @description + * Validates the the supplied value is valid base58 + */ +export default function base58Validate (value: string): true { + for (let i = 0; i < value.length; i++) { + if (!BASE58_ALPHABET.includes(value[i])) { + throw new TypeError(`Invalid base58 character "${value[i]}" (0x${value.charCodeAt(i).toString(16)}) at index ${i}`); + } + } + + return true; +} diff --git a/packages/util-crypto/src/base64/decode.spec.ts b/packages/util-crypto/src/base64/decode.spec.ts new file mode 100644 index 000000000..19a81f4f2 --- /dev/null +++ b/packages/util-crypto/src/base64/decode.spec.ts @@ -0,0 +1,17 @@ +// Copyright 2017-2020 @polkadot/util-crypto authors & contributors +// This software may be modified and distributed under the terms +// of the Apache-2.0 license. See the LICENSE file for details. + +import { stringToU8a } from '@polkadot/util'; + +import { base64Decode } from '.'; + +describe('base64Decode', (): void => { + it('decodes a mixed base64 utf8 string', (): void => { + expect( + base64Decode('aGVsbG8gd29ybGQg0J/RgNC40LLQtdGC0YHRgtCy0YPRjiDQvNC4IOS9oOWlvQ==') + ).toEqual( + stringToU8a('hello world Приветствую ми 你好') + ); + }); +}); diff --git a/packages/util-crypto/src/base64/decode.ts b/packages/util-crypto/src/base64/decode.ts new file mode 100644 index 000000000..22496ba13 --- /dev/null +++ b/packages/util-crypto/src/base64/decode.ts @@ -0,0 +1,19 @@ +// Copyright 2017-2020 @polkadot/util-crypto authors & contributors +// This software may be modified and distributed under the terms +// of the Apache-2.0 license. See the LICENSE file for details. + +import { bufferToU8a } from '@polkadot/util'; + +import validate from './validate'; + +/** + * @name base64Decode + * @summary Decodes a base64 value. + * @description + * From the provided input, decode the base64 and return the result as an `Uint8Array`. + */ +export default function base64Decode (value: string): Uint8Array { + validate(value); + + return bufferToU8a(Buffer.from(value, 'base64')); +} diff --git a/packages/util-crypto/src/base64/encode.spec.ts b/packages/util-crypto/src/base64/encode.spec.ts new file mode 100644 index 000000000..305350887 --- /dev/null +++ b/packages/util-crypto/src/base64/encode.spec.ts @@ -0,0 +1,13 @@ +// Copyright 2017-2020 @polkadot/util-crypto authors & contributors +// This software may be modified and distributed under the terms +// of the Apache-2.0 license. See the LICENSE file for details. + +import { base64Encode } from '.'; + +describe('base64Encode', (): void => { + it('encodes a mixed base64 utf8 string', (): void => { + expect( + base64Encode('hello world Приветствую ми 你好') + ).toEqual('aGVsbG8gd29ybGQg0J/RgNC40LLQtdGC0YHRgtCy0YPRjiDQvNC4IOS9oOWlvQ=='); + }); +}); diff --git a/packages/util-crypto/src/base64/encode.ts b/packages/util-crypto/src/base64/encode.ts new file mode 100644 index 000000000..dd376b86a --- /dev/null +++ b/packages/util-crypto/src/base64/encode.ts @@ -0,0 +1,15 @@ +// Copyright 2017-2020 @polkadot/util-crypto authors & contributors +// This software may be modified and distributed under the terms +// of the Apache-2.0 license. See the LICENSE file for details. + +import { u8aToBuffer, u8aToU8a } from '@polkadot/util'; + +/** + * @name base64Encode + * @summary Creates a base64 value. + * @description + * From the provided input, create the base64 and return the result as a string. + */ +export default function base58Encode (value: Uint8Array | string | Buffer | number[]): string { + return u8aToBuffer(u8aToU8a(value)).toString('base64'); +} diff --git a/packages/util-crypto/src/base64/index.ts b/packages/util-crypto/src/base64/index.ts new file mode 100644 index 000000000..81dc4bbfd --- /dev/null +++ b/packages/util-crypto/src/base64/index.ts @@ -0,0 +1,11 @@ +// Copyright 2017-2020 @polkadot/util-crypto authors & contributors +// This software may be modified and distributed under the terms +// of the Apache-2.0 license. See the LICENSE file for details. + +/** + * @summary Encode and decode base64 values + */ + +export { default as base64Decode } from './decode'; +export { default as base64Encode } from './encode'; +export { default as base64Validate } from './validate'; diff --git a/packages/util-crypto/src/base64/validate.spec.ts b/packages/util-crypto/src/base64/validate.spec.ts new file mode 100644 index 000000000..db38b7b24 --- /dev/null +++ b/packages/util-crypto/src/base64/validate.spec.ts @@ -0,0 +1,13 @@ +// Copyright 2017-2020 @polkadot/util-crypto authors & contributors +// This software may be modified and distributed under the terms +// of the Apache-2.0 license. See the LICENSE file for details. + +import { base64Validate } from '.'; + +describe('base64Validate', (): void => { + it('decodes a mixed base64 utf8 string', (): void => { + expect( + () => base64Validate('aGVsbG8gd29ybGQg0J/RgNC40LLQtd^GC0YHRgtCy0YPRjiDQvNC4IOS9oOWlvQ==') + ).toThrow(/Invalid base64 encoding/); + }); +}); diff --git a/packages/util-crypto/src/base64/validate.ts b/packages/util-crypto/src/base64/validate.ts new file mode 100644 index 000000000..ac605b242 --- /dev/null +++ b/packages/util-crypto/src/base64/validate.ts @@ -0,0 +1,17 @@ +// Copyright 2017-2020 @polkadot/util-crypto authors & contributors +// This software may be modified and distributed under the terms +// of the Apache-2.0 license. See the LICENSE file for details. + +/** + * @name base64Validate + * @summary Validates a base64 value. + * @description + * Validates the the supplied value is valid base64 + */ +export default function base64Validate (value: string): true { + if (!(/^(?:[A-Za-z0-9+/]{2}[A-Za-z0-9+/]{2})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$/.test(value))) { + throw new TypeError('Invalid base64 encoding'); + } + + return true; +} diff --git a/packages/util-crypto/src/index.ts b/packages/util-crypto/src/index.ts index a1b8e50d5..5ec6296e3 100644 --- a/packages/util-crypto/src/index.ts +++ b/packages/util-crypto/src/index.ts @@ -11,6 +11,7 @@ export * from './init'; // all internal exports export * from './address'; export * from './base58'; +export * from './base64'; export * from './blake2'; export * from './keccak'; export * from './key';