DOCUMENTATION Crypt
Overview
Getting Started
BASE
Reference
Worker
Setup modules
COMMODETTO
Reference
Poco Renderer
Outlines
Creating Fonts
Crypt
Data
DEVICES
Moddable One
Moddable Two
Moddable Three
Moddable Four
ESP32
ESP8266
nRF52
nRF52 Low Power Notes
Raspberry Pi Pico
M5Core Ink
M5Paper
Wasm
SiLabs Gecko
QCA4020
Moddable Zero
DISPLAYS
Overview
Adafruit 1.8" ST7735
Adafruit OLED Display
BuyDisplay 2.8" CTP - ESP8266
Crystalfontz ePaper
DotStar
Generic 1.44"
Generic 2.4" & 2.8" (Resistive Touch) - ESP32
Generic 2.4" & 2.8" (Resistive Touch) - ESP8266
Generic 2.4" & 2.8" (Resistive Touch) - Pico
Sharp Memory
Sharp Memory Screen 1.3"
SparkFun TeensyView
Switch Science Reflective LCD
DRIVERS
DESTM32S display
DotStar display
ILI9341 display
LPM013M126A display
LS013B4DN04 display
MCP230XX GPIO expander
NeoStrand
SSD1306 display
SSD1351 display
ST7735 display
Files
IO
TC53 IO
Firmata
NETWORK
Reference
TLS (SecureSocket)
BLE
Ethernet
Web Things
PINS
Reference
Audio Output
PIU
Reference
Localization
Keyboard
Expanding Keyboard
Die Cut
Using After Effects Motion Data
TOOLS
Reference
Manifest
Defines in Manifests
Testing
XS
Handle
JavaScript language considerations on embedded devices using the XS engine
Mods – User Installed Extensions
ROM Colors
Using XS Preload to Optimize Applications
XS Conformance
XS Marshalling
XS Platforms
XS in C
XS linker warnings
xsbug
xst
XS Compartment
XS Profiler

Crypt

Copyright 2017-2022 Moddable Tech, Inc.
Revised: June 14, 2022

class Digest (Crypt)

The Digest class creates cryptographic hashes using a variety of algorithms.

import {Digest} from "crypt";

MD5 Hash

let digest = new Digest("MD5");
digest.write("hello, world);
trace(`MD5 Hash: ${digest.close()}\n`);

SHA1 Hash

let digest = new Digest("SHA1");
digest.write("hello,");
digest.write(" world");
trace(`SHA1 Hash: ${digest.close()}\n`);

new Digest(type)

The Digest constructor takes the type of the hash to calculate as its sole argument.

let digest = new Digest("SHA1");

The following hash functions are supported:

  • MD5
  • SHA1
  • SHA224
  • SHA256
  • SHA384
  • SHA512

write(message)

The write function adds a message to the hash being calculated. There is no restriction on the length of the message. The message argument to write may be a String or ArrayBuffer. The write function may be called more than once for a given digest calculation.

digest.write("123");
digest.write("456");

close()

The close function returns the the calculated hash. The hash is returned as an ArrayBuffer. The close function may only be called once, as it frees all resources associated with the digest calculation.

class Transform

The Transform class contains static methods to perform common transformations of certificate data.

import Transform from "crypt/transform";

Whenever possible, transformation of data should not be performed at runtime because it uses additional time and memory. Instead, the data should be stored in the optimal format for the target device. These transformation functions are provided for situations where the transformation must be performed at runtime, such as some device provisioning flows.

Add the Transform module in a manifest to use it in a project.

"modules": {
	"crypt/transform": "$(MODULES)/crypt/etc/transform"
}

static pemToDER(data)

The pemToDER function transforms a certificate in PEM format (Base64 encoded ASCII) to DER format (binary data). The input data may be a String, ArrayBuffer, or host buffer. The return value is an ArrayBuffer.

For PEM files containing more than one certificate, pemToDER converts only the first certificate.

This function is similar to the following openssl command line:

openssl x509 -inform pem -in data.pem -out data.der -outform der

pemToDER looks for both -----BEGIN CERTIFICATE----- and -----BEGIN RSA PRIVATE KEY----- as delimeters.

static privateKeyToPrivateKeyInfo(data[, oid])

The privateKeyToPrivateKeyInfo function transforms a Private Key to a Private Key Info (both in binary DER format). The input data is an ArrayBuffer or host buffer. The optional oid parameter is the Object ID for the key algorithm as an Array. If not provided, it defaults to the OID for PKCS#1, [1, 2, 840, 113549, 1, 1, 1]. The return value is an ArrayBuffer.

Using pemToDER and privateKeyToPrivateKeyInfo together is similar to the following openssl command line:

openssl pkcs8 -topk8 -in private_key.pem -inform pem -out private_key.pk8.der -outform der -nocrypt