SimpleCrypto (simple_crypto v1.0.5) View Source

Simple crypto helpers for Elixir.

Link to this section Summary

Functions

Decrypts str using key as the decryption key.

Encrypts str using key as the encryption key.

Use key to generate a hash value of str using the HMAC method.

Generate a random string of length length that is suitable to use as an easily copy/pastable ID (no hyphens or other problematic characters).

Generate a random string of length length that is suitable to use as an OTP (One-time pad) code.

DEPRECATED: Use pad_by_width/3 instead.

Pad the end of str using padding, so that the total length becomes a multiple of width.

Generate a random string of length length, consisting only of integers.

Generate a random string of length length.

Hashes str using the SHA256 algorithm.

Link to this section Functions

Specs

decrypt(iodata(), iodata()) :: binary()

Decrypts str using key as the decryption key.

Example

iex> SimpleCrypto.decrypt("qCgs4rfReY5nTX39uHwjww==", "secret key")
"Hi there"

Specs

encrypt(iodata(), iodata()) :: binary()

Encrypts str using key as the encryption key.

Example

iex> SimpleCrypto.encrypt("Hi there", "secret key")
"qCgs4rfReY5nTX39uHwjww=="

Specs

hmac(iodata(), iodata()) :: binary()

Use key to generate a hash value of str using the HMAC method.

Example

iex> SimpleCrypto.hmac("HMAC me now!", "secret key")
"E7235176D81E29EC202B117324C7B3A2A6180F2A2A163D79E5A6BB58E7A61A7B"

Specs

id_rand_str(pos_integer()) :: binary()

Generate a random string of length length that is suitable to use as an easily copy/pastable ID (no hyphens or other problematic characters).

Example

iex> SimpleCrypto.id_rand_str(12)
"SWm6fDWvd4id"
Link to this function

otp_rand_str(length \\ 16)

View Source

Specs

otp_rand_str(pos_integer()) :: binary()

Generate a random string of length length that is suitable to use as an OTP (One-time pad) code.

Example

iex> SimpleCrypto.otp_rand_str(16)
"UXGMUXNUANHONKZR"
Link to this function

pad(str, width \\ 16, padding \\ " ")

View Source

Specs

pad(iodata(), pos_integer(), iodata()) :: binary()

DEPRECATED: Use pad_by_width/3 instead.

Link to this function

pad_by_width(str, width \\ 16, padding \\ " ")

View Source

Specs

pad_by_width(iodata(), pos_integer(), iodata()) :: binary()

Pad the end of str using padding, so that the total length becomes a multiple of width.

Example

iex> SimpleCrypto.pad_by_width("The length of this string is 76 before padding, 4 less than a multiple of 16", 16, ".")
"The length of this string is 76 before padding, 4 less than a multiple of 16...."
Link to this function

rand_int_str(num_of_digits)

View Source

Specs

rand_int_str(pos_integer()) :: binary()

Generate a random string of length length, consisting only of integers.

Example

iex> SimpleCrypto.rand_int_str(6)
"811238"

Specs

rand_str(pos_integer()) :: binary()

Generate a random string of length length.

Example

iex> SimpleCrypto.rand_str(32)
"rvbAtDMdVPJu2J-QDyAxgOLAL0LQWL0w"

Specs

sha256(iodata()) :: binary()

Hashes str using the SHA256 algorithm.

Example

iex> SimpleCrypto.sha256("Turn me into SHA256")
"87A3AABED406EFBCD4956E2E32E75948DB88E7ED35CACD4D8B66669EA849C102"