convert

Manages data format conversion between a variety of targets.

converthint, inputoutput

nametypedescription

hint

Object

An object with the keys from and to. Their values describe the conversion to be applied to input.

input

The value to be converted.

output

The result of the conversion.

Behind the scenes, Confidential generics, like encrypt operate on byte arrays. The purpose of convert is to make it easier to convert between byte arrays and other formats, like UTF-8 strings.

However, you will not typically need to use convert directly because Confidential also provides from and to convenience methods for most classes, which call convert for you behind the scenes.

A conversion is specified by the fields of hint, from and to. These identify the current and target format of input, respectively.

Supported Hints

Errors

convert is meant to provide safe and correct format conversion for your data. There are a number of checks in place that will throw errors if they fail:

Example

import assert from "assert"
import {confidential} from "panda-confidential"
{convert} = confidential()

assert.equal "Hello, World!",
  convert from: "base64", to: "utf8", "SGVsbG8sIFdvcmxkIQ=="

assert.equal (Buffer.from "Hello, World!"),
  convert from: "utf8", to: "bytes" , "Hello, World!"

assert.equal "Hello, World!",
  convert from: "bytes", to: "utf8", Buffer.from "Hello, World!"