Skip to content

Commit

Permalink
Added is_decryped and working on documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
aforward committed Dec 14, 2014
1 parent 009e2be commit dab092d
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 4 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
/deps
erl_crash.dump
*.ez
docs
31 changes: 29 additions & 2 deletions lib/safetybox.ex
Original file line number Diff line number Diff line change
@@ -1,14 +1,41 @@
defmodule Safetybox do

@moduledoc """
# Safetybox a collection of security oriented functions
## Dependency
{ :safetybox, "~> 0.1" }
## Usage
### API
pwd = Safetybox.encrypt("helloworld")
Safetybox.is_decrypted("goodbyeworld", pwd) # > returns false
Safetybox.is_decrypted("helloworld", pwd) # > returns true
## Author
Copyright © 2014 Andrew Forward
@a4word, [email protected]
Licensed under MIT.
"""

# Taken from https://gist.github.com/10nin/5713366
def encrypt(nil), do: encrypt("")
def encrypt(str) do
:crypto.hash(:md5, str)

@doc """
Convert a plaintext string into an encrypted string, for example
Safetybox.encrypt("helloworld")
"""
def encrypt(plaintext) do
:crypto.hash(:md5, plaintext)
|> :erlang.bitstring_to_list
|> Enum.map(&(:io_lib.format("~2.16.0b", [&1])))
|> List.flatten
|> :erlang.list_to_bitstring
end

@doc """
Compare a plaintext string to it's encrypted version to test if they match.
Safetybox.is_decrypted("helloworld", "dasdf!@#CASD")
"""
def is_decrypted(plaintext, encrypted), do: encrypt(plaintext) == encrypted

end
10 changes: 8 additions & 2 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@ defmodule Safetybox.Mixfile do
def project do
[
app: :safetybox,
version: "0.0.1",
version: "0.0.2",
elixir: "~> 1.0",
deps: deps,

name: "safetybox",
source_url: "https://github.com/aforward/safetybox",
package: [
contributors: ["Andrew Forward"],
licenses: ["MIT"],
Expand Down Expand Up @@ -35,6 +38,9 @@ defmodule Safetybox.Mixfile do
#
# Type `mix help deps` for more examples and options
defp deps do
[]
[
{:ex_doc, "~> 0.6", only: :dev},
{:earmark, "~> 0.1"},
]
end
end
2 changes: 2 additions & 0 deletions mix.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
%{"earmark": {:hex, :earmark, "0.1.12"},
"ex_doc": {:hex, :ex_doc, "0.6.2"}}
14 changes: 14 additions & 0 deletions test/safetybox_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,18 @@ defmodule SafetyboxTest do
assert S.encrypt("andrew") == S.encrypt("andrew")
end

test "is_decrypted against encrypted value" do
assert S.is_decrypted("helloworld", "XXX") == false
assert S.is_decrypted("helloworld", S.encrypt("andrew")) == false
assert S.is_decrypted("helloworld", S.encrypt("helloworld")) == true
end

test "is_decrypted should handle nil" do
assert S.is_decrypted(nil, "XXX") == false
assert S.is_decrypted("helloworld", nil) == false
assert S.is_decrypted(nil, nil) == false
assert S.is_decrypted(nil, S.encrypt(nil)) == true
assert S.is_decrypted("", S.encrypt("")) == true
end

end

0 comments on commit dab092d

Please sign in to comment.