Simple Cipher

A lightweight and portable cryptography library

API / Library

Simple Cipher is a lightweight and portable cryptography library providing a minimal degree of security for non mission-critical data (e.g. unique, non-guessable URLs).

Also included is a pure Lua-adaptation of XTEA, a public domain block-cipher algorithm designed by Needham and Wheeler in 1997.

The mod may be installed either as a Minetest mod or as a standalone library for use in command-line scripts. The following functions are made available:

cipher.get_checksum( str, method )

Calculates and returns the numeric hash of the string using one of these methods:

  • "fletcher64" for the Fletcher-64 algorithm
  • "fletcher32" for the Fletcher-32 algorithm
  • "fletcher16" for the Fletcher-16 algorithm
  • "adler32" for the Adler-32 algorithm (default)

cipher.tokenize( hash )

Given a numeric hash, this function generates and returns a token consisting only of letters in the alphabet soup. This is primary useful for generating short-URLs, in which the hash corresponds to a unique key within a database of site redirects.

cipher.generate_key( username, password )

Generates and returns a 128-bit public/private key pair for use with either of the cryptography functions described below. This key is intended only for encrpytion and decryption, and should NEVER be shared under any circumstances. If a password is not provided, then the value of cipher.password will be used by default.

cipher.encrypt( num, str, key )

Encrypts the string using the XTEA block-cipher and returns the ciphertext. The key should be generated by cipher.generate_key(), and an appropriate number of rounds chosen to mitigate against attacks (32 or more is recommended, 64 is maximum).

cipher.decrypt( str, key )

Decrypts the string that was previously encrypted by cipher.encrypt() with the same key generated by cipher.generate_key(). The enrypted string will be checked both for proper length and valid header prior to decryption.

cipher.encrypt_to_base64( str, username )

This is a convenience function for encryption of a string with just a username. It returns a Base-64 string representation of the ciphertext.

cipher.decrypt_from_base64( str, username )

This is the inverse of cipher.encrypt_to_base64, and therefore expects a Base-64 string representation of the ciphertext as well as the original username.

Before you begin, it is important that you customize the alphabet soup that will be used by the tokenizer. Likewise, if you intend to use the XTEA block-cipher, then a password needs to be set. Both variables can be found at the head of the init.lua file.



Do you recommend this mod?

  • No reviews, yet.

Used By