cipher

ROT13, Caesar, Vigenere, repeating-key XOR, Atbash and constant-time compare.

v1.0.0Security & encodingbundledimport "cipher";
Overview Installation Documentation Examples

Overview

A zero-dependency classical and utility cipher toolkit for HolyPHP.

Includes ROT13, Caesar, Vigenere, repeating-key XOR, Atbash, and constant-time string equality.

bundled — works offline zero dependencies rot13 caesar

Quick start

import "cipher";

$rot = cipher_rot13("Hello, World!"); // "Uryyb, Jbeyq!"
$enc = cipher_caesar_encrypt("Attack at dawn!", 5);
$dec = cipher_caesar_decrypt($enc, 5); // "Attack at dawn!"

$vig = cipher_vigenere_encrypt("Hello, World!", "KEY");
$vigDec = cipher_vigenere_decrypt($vig, "KEY"); // "Hello, World!"

$xored = cipher_xor("secret message", "mykey");
$restored = cipher_xor($xored, "mykey"); // "secret message"

$atb = cipher_atbash("Abc Xyz!"); // "Zyx Cba!"

$same = cipher_constant_time_equals("token123", "token123"); // true

Installation

In a project

Inside a project (a folder with hphp.yaml, see Projects & packages):

hphp install cipher

This adds the library to hphp.yaml, installs it into .hphp/packages/cipher/ and pins the exact version in hphp.lock:

require:
  cipher: "^1.0"

Use it

import "cipher";

cipher is bundled with the compiler, so import "cipher"; also works in a single file without installing anything, and hphp install works offline.

Outside a project

hphp install cipher          # for your user, shared by all scripts
hphp create myapp                 # or start a project, then: hphp install cipher
Packagecipher
Version1.0.0 (follows the compiler)
Entry filecipher.hphp
Sourcepackages/lib/cipher ↗

Documentation

API Reference

cipher_rot13(string $text): string

Applies the ROT13 substitution cipher to ASCII letters (A-Z, a-z). Non-letter characters are preserved unchanged. Applying cipher_rot13 twice returns the original string.

cipher_caesar_encrypt(string $text, int $shift): string

Encrypts $text using a Caesar shift of $shift positions (supports negative and large shifts modulo 26). Preserves case and leaves non-ASCII-letter characters unchanged.

cipher_caesar_decrypt(string $text, int $shift): string

Decrypts $text encrypted with cipher_caesar_encrypt($text, $shift).

cipher_vigenere_encrypt(string $text, string $key): string

Encrypts $text using the Vigenere cipher with alphabetic key $key. Case is preserved and non-letter characters in $text are passed through without advancing the key index.

cipher_vigenere_decrypt(string $text, string $key): string

Decrypts $text encrypted with cipher_vigenere_encrypt($text, $key).

cipher_xor(string $data, string $key): string

Applies a repeating-key bitwise XOR between $data and $key. Applying cipher_xor twice with the same key restores the original data.

cipher_atbash(string $text): string

Applies the Atbash cipher (A <-> Z, a <-> z). Non-letter characters are preserved unchanged. Applying cipher_atbash twice returns the original string.

cipher_constant_time_equals(string $a, string $b): bool

Compares two strings without early exit on byte mismatch, returning true if and only if they have the same length and identical bytes.

Error Handling

All validation errors throw Exception with clear, prefixed messages:

Examples

01Quick start

Bundled with the compiler — import "cipher"; works offline in any file, or add it to a project with hphp install cipher.

import "cipher";

$rot = cipher_rot13("Hello, World!"); // "Uryyb, Jbeyq!"
$enc = cipher_caesar_encrypt("Attack at dawn!", 5);
$dec = cipher_caesar_decrypt($enc, 5); // "Attack at dawn!"

$vig = cipher_vigenere_encrypt("Hello, World!", "KEY");
$vigDec = cipher_vigenere_decrypt($vig, "KEY"); // "Hello, World!"

$xored = cipher_xor("secret message", "mykey");
$restored = cipher_xor($xored, "mykey"); // "secret message"

$atb = cipher_atbash("Abc Xyz!"); // "Zyx Cba!"

$same = cipher_constant_time_equals("token123", "token123"); // true
Try it
hphp install cipher
hphp run main.hphp

Covered by the official test suite in tests/positive/lib_cipher.hphp.