Skip to content

Cryptographic Accelerator

The Cryptographic Accelerator is a peripheral that adds cryptographic functions and utilities that ComputerCraft may be too slow or incompatible to implement to.

Cryptographic Accelerator

Methods

The network name of the Cryptographic Accelerator peripheral is cryptographic_accelerator, you can wrap it via the peripheral functions like peripheral.wrap or peripheral.find.

Hashing

Note: hex arguments are by default true unless explicitly set.

  • md5(data: string, [hex: boolean]): string Hash data with the MD5 algorithm.
  • sha1(data: string, [hex: boolean]): string Hash data with the SHA1 algorithm.
  • sha256(data: string, [hex: boolean]): string Hash data with the SHA256 algorithm.
  • sha512(data: string, [hex: boolean]): string Hash data with the SHA512 algorithm.
  • hmacMd5(key: string, data: string, [hex: boolean]): string Hash data with the MD5 algorithm and HMAC key.
  • hmacSha1(key: string, data: string, [hex: boolean]): string Hash data with the SHA1 algorithm and HMAC key.
  • hmacSha256(key: string, data: string, [hex: boolean]): string Hash data with the SHA256 algorithm and HMAC key.
  • hmacSha512(key: string, data: string, [hex: boolean]): string Hash data with the SHA512 algorithm and HMAC key.

BLAKE3 (v0.6.1+)

  • blake3(data: string, [hex: boolean], [mode: string], [param: string], [outputSize: number]): string Hash data with the BLAKE3 algorithm.
Modes
  • hash: Hash data. The param argument is ignored.
  • keyed: Hash data with a key, requires param argument to be a 32-bytes string key.
  • derive: Hash data with a context string, requires param argument to be a string of any length.
Output Size

The BLAKE3 algorithm allows to set the output size of the digest (returned string). By default it is 32 bytes (256 bit). The output size is limited up to 4096 bytes for ComputerCraft performance reasons.

Random

  • random(): number Generate a cryptographically secure random number.
  • random(max: number): number Generate a cryptographically secure random number between 0 and max (exclusive).
  • random(min: number, max: number): number Generate a cryptographically secure random number between min (inclusive) and max (exclusive).
  • randomBytes(length: number): string Generate a string composed of securely randomly generated bytes.

Encoding

  • encodeBase64(data: string): string Encode a string to Base64.
  • decodeBase64(data: string): string Decode a Base64 string or return nil if input is invalid.

Symmetric Encryption

  • encryptAes(data: string, key: string, iv: string): string Encrypt a string using AES algorithm with CBC.
  • decryptAes(data: string, key: string, iv: string): string Decrypt a string using AES algorithm with CBC.

Asymmetric Signing and Key Exchange

Note: Curve25519 (Elliptic Curve) algorithm is used in the following methods.

  • generatePrivateKey(): string Generate a new private key for Ed25519 and X25519.

Signing functions (Ed25519)

  • derivePublicKey(privateKey: string): string Derive the Ed25519 public key from a private key.
  • sign(message: string, privateKey: string): string Sign a message using a private key.
  • verify(message: string, signature: string, publicKey: string): boolean Verify whether a message has been signed with the correct key pair.

Key Exchange functions (X25519)

  • deriveEcdhPublicKey(privateKey: string): string Derive the X25519 public key from a private key.
  • computeSharedSecret(privateKey: string, peerPublicKey: string): string Use your private key and the peer's public key to compute the shared secret.

Compression (v0.6.0+)

  • deflate(data: string): string Compress data using the DEFLATE compression algorithm.
  • inflate(data: string): string Decompress data using the DEFLATE compression algorithm.