Created
October 10, 2012 10:16
-
-
Save mrnovalles/3864572 to your computer and use it in GitHub Desktop.
Erlang signing and verifying hashed data, using openssh keys
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
-module(signature). | |
-export([test/0]). | |
% Looking at using Open SSH public and private keys | |
% for signing and verifying hashed data. | |
load_public_key(Filename) -> | |
{ok, SshBin} = file:read_file(Filename), | |
[{PubKey,_}] = public_key:ssh_decode(SshBin, public_key), | |
PubKey. | |
load_private_key(Filename) -> | |
{ok, Pem} = file:read_file(Filename), | |
[Entry] = public_key:pem_decode(Pem), | |
public_key:pem_entry_decode(Entry). | |
sign_verify(PublicKey, PrivateKey, Digest) -> | |
Signature = public_key:sign(Digest, sha, PrivateKey), | |
true = public_key:verify(Digest, sha, Signature, PublicKey). | |
test() -> | |
Msg = "This is the message", | |
Digest = crypto:sha(Msg), | |
PublicKey = load_public_key("id_rsa.pub"), | |
PrivateKey = load_private_key("id_rsa"), | |
sign_verify(PublicKey, PrivateKey, Digest). |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment