forked from dynamo/dynamo
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
José Valim
committed
Aug 24, 2012
1 parent
b222861
commit 28895e9
Showing
8 changed files
with
105 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
defmodule Dynamo do | ||
def start do | ||
:application.start(:mimetypes) | ||
:application.start(:crypto) | ||
:application.start(:dynamo) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
defmodule Dynamo.Utils.MessageVerifier do | ||
@moduledoc """ | ||
`MessageVerifier` makes it easy to generate and verify messages | ||
which are signed to prevent tampering. | ||
For example, the session store uses this verifier to send data | ||
to the client. Although the data can be read by the client, he | ||
cannot tamper it. | ||
""" | ||
|
||
@doc """ | ||
Decodes and verifies the encoded binary was not tampared with. | ||
""" | ||
def verify(encoded, secret) do | ||
case Binary.split(encoded, "--") do | ||
[content, digest] when content != "" and digest != "" -> | ||
if secure_compare(digest(secret, content), digest) do | ||
{ :ok, content /> :base64.decode /> binary_to_term } | ||
else | ||
:error | ||
end | ||
_ -> | ||
:error | ||
end | ||
end | ||
|
||
@doc """ | ||
Generates an encoded and signed binary for the given term. | ||
""" | ||
def generate(term, secret) do | ||
encoded = term /> term_to_binary /> :base64.encode | ||
encoded <> "--" <> digest(secret, encoded) | ||
end | ||
|
||
defp digest(secret, data) do | ||
<<mac | 160 - :integer>> = :crypto.sha_mac(secret, data) | ||
:erlang.integer_to_list(mac, 16) /> list_to_binary | ||
end | ||
|
||
@doc """ | ||
Comapres the two binaries completely, byte by byte, | ||
to avoid timing attacks. | ||
""" | ||
def secure_compare(left, right) do | ||
if size(left) == size(right) do | ||
compare_each(left, right, true) | ||
else | ||
false | ||
end | ||
end | ||
|
||
defp compare_each(<<h, left | :binary>>, <<h, right | :binary>>, acc) do | ||
compare_each(left, right, acc) | ||
end | ||
|
||
defp compare_each(<<_, left | :binary>>, <<_, right | :binary>>, _acc) do | ||
compare_each(left, right, false) | ||
end | ||
|
||
defp compare_each(<<>>, <<>>, acc) do | ||
acc | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
Code.require_file "../../../test_helper.exs", __FILE__ | ||
|
||
defmodule Dynamo.Utils.MessageVerifierTest do | ||
use ExUnit.Case | ||
|
||
alias Dynamo.Utils.MessageVerifier, as: MV | ||
|
||
test "generates a signed message" do | ||
[content, encoded] = Binary.split MV.generate(:hello, "secret"), "--" | ||
assert content /> :base64.decode /> binary_to_term == :hello | ||
assert size(encoded) == 40 | ||
end | ||
|
||
test "verifies a signed message" do | ||
signed = MV.generate(:hello, "secret") | ||
assert MV.verify(signed, "secret") == { :ok, :hello } | ||
end | ||
|
||
test "does not verify a signed message if secret changed" do | ||
signed = MV.generate(:hello, "secret") | ||
assert MV.verify(signed, "secreto") == :error | ||
end | ||
|
||
test "does not verify a tampered message" do | ||
[_, encoded] = Binary.split MV.generate(:hello, "secret"), "--" | ||
content = :bye /> term_to_binary /> :base64.encode | ||
assert MV.verify(content <> "--" <> encoded, "secret") == :error | ||
end | ||
end |
16 changes: 8 additions & 8 deletions
16
test/dynamo/support/once_test.exs → test/dynamo/utils/once_test.exs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters