Current section

Files

Jump to
apollo18 lib apollo18 based64 based64_cipher.ex
Raw

lib/apollo18/based64/based64_cipher.ex

defmodule Apollo18.Based64Cipher do
@moduledoc """
Provides cipher mechanism to make your things be like Apollo 18:
Officially don't exists, only the owner know that it (if ?) exists.
The truth is out there (Maybe ...).
Uses the ```Apollo18.Cipher``` mechanism, applying additional steps,
to encode/decode to/from base64 before/after cipher/decipher.
This module functionalities are present in web/javascript version.
This could be usefull to send/receive messages between a web browser
client (Javascript side) and a server (Elixir side).
For javascript instantiation use:
```javascript
let cipher = new Based64Cipher();
```
Remember of take additional care to handle securely ```key```
and ```salt``` values when use it whit javascript. Rotate the key
each X minutes and store it ciphered for example.
If you want maximum security try use one new key per message.
Never, never, never use sequential keys, always randomize, please!
"""
alias Apollo18.Cipher
@doc """
Same doc as for ```Apollo18.Cipher```. Only add a base64 encode conversion
to received text/string before call Apollo18.Cipher.cipher(string,salt).
"""
@doc since: "1.0.0"
def cipher(string,salt \\ 1,key \\ 0) do
string
|> Base.encode64()
|> Cipher.cipher(salt,key)
end
@doc """
Same doc as for ```Apollo18.Cipher```. Only add a base64 decode conversion
to received text/string after receive the result of call Apollo18.Cipher.decipher(string,salt).
"""
@doc since: "1.0.0"
def decipher(string,salt \\ 1,key \\ 0) do
Cipher.decipher(string,salt,key)
|> Base.decode64()
|> get_decoded_result()
end
@doc false
defp get_decoded_result(:error) do
"ERROR"
end
@doc false
defp get_decoded_result(base64) do
base64
|> Tuple.to_list()
|> Enum.at(1)
end
end