Encryption in Clojure
Add the following in the dependencies section of your project.clj file
:dependencies [...
[azjure "1.0.0-SNAPSHOT"]
...]Each function in the API uses map to configure the behavior of the encrypt/decrypt functions.
The map has the following format:
{:type :typekw
:mode :modekw
:pad :padderkw
:eid :input-decoderkw
:eoe :output-encoderkw
:did :input-decoderkw
:doe :output-encoderkw
:key []
:iv []}- type - A keyword that identifies the cipher you wish to use. See cipher.clj for supported values.
- mode - A keyword that identifies the block chaining mode you wish to use. See modes.clj for supported values.
- pad - A keyword that identifies the padder you wish to use. See padders.clj for supported values.
- eid - A keyword that represents the encryption input decoder you wish to use. See encoders.clj for supported values.
- eoe - A keyword that represents the encryption output encoder you wish to use. See encoders.clj for supported values.
- did - A keyword that represents the decryption input decoder you wish to use. See encoders.clj for supported values.
- doe - A keyword that represents the decryption output encoder you wish to use. See encoders.clj for supported values.
- key - A vector of unsigned bytes (0-255) of the appropriate length that represents the key you wish to use with the cipher.
- iv - A vector of unsigned bytes (0-255) of the appropriate length that represents the IV you wish to use with the block chaining mode.
- nonce - A vector of unsigned bytes (0-255) of the appropriate length that represents the nonce you with to use with the stream cipher.
(:require ...
[azjure.core :refer :all]
[azjure.cipher.aes :refer :all]
;Require all the cipher(s) you wish to use
...
)Encrypt
;; Encrypt a vector of unsigned bytes
;; Note that the keys shown below are the required keys for a
;; block cipher
(encrypt [0 0 0 0]
{:type :aes :mode :ecb :pad :pkcs7
:key [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
:iv [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]})
;; Should evaluate to
;; [223 80 151 26 46 117 190 64 134 255 95 229 221 229 165 35]Decrypt
;; Decrypt a vector of unsigned bytes
(decrypt [223 80 151 26 46 117 190 64 134 255 95 229 221 229 165 35]
{:type :aes :mode :ecb :pad :pkcs7
:key [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
:iv [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]})
;; Should evaluate to [0 0 0 0](:require ...
[azjure.core :refer :all]
[azjure.cipher.salsa20 :refer :all]
;Require all the ciphers(s) you wish to use
...
)Encrypt/Decrypt
;; Generate ciphertext
;; Note that the keys shown below are the required keys for
;; a stream cipher
(encrypted-stream [0 0 0 0]
{:type :salsa20
:key [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
:nonce [0 0 0 0 0 0 0 0]})
;; Should evaluate to [101 19 173 174]
;; Generate plaintext
(encrypted-stream [101 19 173 174]
{:type :salsa20
:key [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
:nonce [0 0 0 0 0 0 0 0]})
;; Should evaluate to [0 0 0 0]See the examples documentation for more extensive usage examples
- Advanced Encryption Standard (AES) - FIPS 197
- Blowfish (BF) - Blowfish Spec
- CAST-256 (CAST6) - CAST-256 RFC
- Twofish (TF) - Twofish Spec
- TEA (TEA) - TEA Spec
- XTEA (XTEA) - XTEA Spec
- CAST-128 (CAST5) - CAST-128 RFC
- Salsa20 (Salsa20) - Salsa20 Spec
- ChaCha (Chacha) - ChaCha Spec
- HC-128 (HC128) - HC-128 Spec
- HC-256 (HC256) - HC-256 Spec
- MICKEY2.0 (MICKEY2.0) - MICKEY2.0 Spec
- Rabbit (Rabbit) - Rabbit Spec
- Trivium (Trivium) - Trivium Spec
Cipher modes describe the method for encrypting multiple blocks with block ciphers.
See Mode of Operation for descriptions
- Electronic Codebook (ECB)
- Cipher-Block Chaining (CBC)
- Propagating Cipher-Block Chaining (PCBC)
- Cipher Feedback (CFB)
- Output Feedback (OFB)
- Counter (CTR)
Some cipher modes (ECB, CBC, PCBC) require that the input be padded with bytes until a multiple of the cipher's blocksize. The following padding methods are supported.
See Padding for descriptions
- PKCS7
- Zero Byte
- ISO 10126
- ANSI X.923
- ISO/IEC 7816-4
By default the API works with vectors of unsigned bytes. However, there is built in support for converting to and from many common character encodings.
The following encodings are supported:
- str - ASCII character encoding
- hex - hex encoding (0-9a-f)
- base16 - Base16 encoding (0-9A-F)
- base32 - Base32 encoding (A-Z2-7)
- base32hex - Base32 encoding with a hex alphabet (0-9A-V)
- base64 - Base64 encoding (A-Za-z0-9+/)
- base64url - Base64 encoding with the URL safe alphabet (A-Za-z0-9-_)