Current section

Files

Jump to
apollo18 lib apollo18 decisor cipher_decisor.ex
Raw

lib/apollo18/decisor/cipher_decisor.ex

defmodule Apollo18.CipherDecisor do
@moduledoc """
Utilitary module to help to choice best cipher parameter
options to be used whit ```Apollo18``` cipher functionality modules.
(This module functionalities are'nt in web/javascript version).
"""
@doc """
Receives a string to be ciphered and the max time in seconds desired
to the resultant ciphered string should take in moment of decipher.
Return the best estimated parameters to be used to decide cipher
configurations.
If these returned parameters aren't good for you, you should
manually inform other parameters to ```Apollo 18``` cipher
functionalities as desired/needed.
Keep in mind that's better slice a long text into 2,3 or more parts
and cipher each part separately if you want be more performatic,
or obtain more ```salt``` application on cipher without increase
exponentially the time to cipher/decipher.
For very small strings (<= 200) the ```salt``` parameter vary from 5
to 6. For small strings (<= 1000) the ```salt``` parameter vary from 3
to 4. For all other cases the ```salt``` parameter vary from 1 to 3.
## Examples
```elixir
iex > string = Krug.SanitizerUtil.generate_random(400)
iex > Apollo18.CipherDecisor.get_best_parameters(string,1)
%{base64: false, salt: 4}
```
```elixir
iex > string = Krug.SanitizerUtil.generate_random(400)
iex > Apollo18.CipherDecisor.get_best_parameters(string,1)
%{base64: false, salt: 4}
```
```elixir
iex > string = Krug.SanitizerUtil.generate_random(400)
iex > Apollo18.CipherDecisor.get_best_parameters(string,1.3)
%{base64: true, salt: 4}
```
```elixir
iex > string = Krug.SanitizerUtil.generate_random(2000)
iex > Apollo18.CipherDecisor.get_best_parameters(string,2)
%{base64: true, salt: 3}
```
```elixir
iex > string = Krug.SanitizerUtil.generate_random(2000)
iex > Apollo18.CipherDecisor.get_best_parameters(string,1)
%{base64: true, salt: 2}
```
```elixir
iex > string = Krug.SanitizerUtil.generate_random(6000)
iex > Apollo18.CipherDecisor.get_best_parameters(string,12)
%{base64: false, salt: 3}
```
```elixir
iex > string = Krug.SanitizerUtil.generate_random(6000)
iex > Apollo18.CipherDecisor.get_best_parameters(string,20)
%{base64: true, salt: 3}
```elixir
iex > string = Krug.SanitizerUtil.generate_random(20000)
iex > Apollo18.CipherDecisor.get_best_parameters(string,16)
%{base64: false, salt: 2}
```
```elixir
iex > string = Krug.SanitizerUtil.generate_random(20000)
iex > Apollo18.CipherDecisor.get_best_parameters(string,20)
%{base64: true, salt: 2}
```
"""
@doc since: "1.0.0"
def get_best_parameters(string,max_decipher_time_execution_seconds \\ 1) do
max_time = cond do
(!(max_decipher_time_execution_seconds > 0)) -> 1
true -> max_decipher_time_execution_seconds
end
length = string |> String.length()
rate = length / max_time
cond do
(length <= 50 and rate <= 40) -> get_salt_and_base64(6,true)
(length <= 50 and rate <= 50) -> get_salt_and_base64(6,false)
(length <= 200 and rate <= 180) -> get_salt_and_base64(5,true)
(length <= 200 and rate <= 200) -> get_salt_and_base64(5,false)
(length <= 400 and rate <= 340) -> get_salt_and_base64(4,true)
(length <= 400 and rate <= 400) -> get_salt_and_base64(4,false)
(length <= 500 and rate <= 230) -> get_salt_and_base64(4,true)
(length <= 500 and rate <= 300) -> get_salt_and_base64(4,false)
(length <= 600 and rate <= 200) -> get_salt_and_base64(4,true)
(length <= 600 and rate <= 260) -> get_salt_and_base64(4,false)
(length <= 700 and rate <= 180) -> get_salt_and_base64(4,true)
(length <= 700 and rate <= 230) -> get_salt_and_base64(4,false)
(length <= 800 and rate <= 150) -> get_salt_and_base64(4,true)
(length <= 800 and rate <= 190) -> get_salt_and_base64(4,false)
(length <= 900 and rate <= 140) -> get_salt_and_base64(4,true)
(length <= 900 and rate <= 180) -> get_salt_and_base64(4,false)
(length <= 1000 and rate <= 125) -> get_salt_and_base64(4,true)
(length <= 1000 and rate <= 150) -> get_salt_and_base64(4,false)
(length <= 1000) -> get_salt_and_base64(3,true)
(length <= 2000 and rate <= 1000) -> get_salt_and_base64(3,true)
(length <= 2000 and rate <= 1300) -> get_salt_and_base64(3,false)
(length <= 3000 and rate <= 750) -> get_salt_and_base64(3,true)
(length <= 3000 and rate <= 1000) -> get_salt_and_base64(3,false)
(length <= 6000 and rate <= 300) -> get_salt_and_base64(3,true)
(length <= 6000 and rate <= 500) -> get_salt_and_base64(3,false)
(length <= 6000) -> get_salt_and_base64(2,true)
(length <= 10000 and rate <= 3300) -> get_salt_and_base64(2,true)
(length <= 10000 and rate <= 5000) -> get_salt_and_base64(2,false)
(length <= 20000 and rate <= 1000) -> get_salt_and_base64(2,true)
(length <= 20000 and rate <= 1300) -> get_salt_and_base64(2,false)
(length <= 20000) -> get_salt_and_base64(1,true)
(length <= 35000 and rate <= 1400) -> get_salt_and_base64(1,true)
true -> get_salt_and_base64(1,false)
end
end
@doc false
defp get_salt_and_base64(salt,base64) do
%{
salt: salt,
base64: base64,
}
end
end