Skip to content
This repository has been archived by the owner. It is now read-only.
Permalink
Browse files
Use public_key-0.8 API (R14B).
  • Loading branch information
tim committed Nov 26, 2010
1 parent 546d0ee commit 2559e8bbf4be816c545a5dd467225a20e0c9dd06
Showing 1 changed file with 15 additions and 14 deletions.
@@ -5,25 +5,26 @@
-include_lib("public_key/include/public_key.hrl").

signature(BaseString, PrivateKeyPath) ->
{ok, [Info]} = public_key:pem_to_der(PrivateKeyPath),
{ok, PrivateKey} = public_key:decode_private_key(Info),
base64:encode_to_string(public_key:sign(list_to_binary(BaseString), PrivateKey)).
{ok, Contents} = file:read_file(PrivateKeyPath),
[Info] = public_key:pem_decode(Contents),
PrivateKey = public_key:pem_entry_decode(Info),
base64:encode_to_string(public_key:sign(list_to_binary(BaseString), sha, PrivateKey)).

verify(Signature, BaseString, PublicKey) ->
public_key:verify_signature(to_binary(BaseString), sha, base64:decode(Signature), public_key(PublicKey)).
verify(Signature, BaseString, Cert) ->
public_key:verify(to_binary(BaseString), sha, base64:decode(Signature), pkey(Cert)).

to_binary(Term) when is_list(Term) ->
list_to_binary(Term);
to_binary(Term) when is_binary(Term) ->
Term.

public_key(Path) when is_list(Path) ->
{ok, [{cert, DerCert, not_encrypted}]} = public_key:pem_to_der(Path),
{ok, Cert} = public_key:pkix_decode_cert(DerCert, otp),
public_key(Cert);
public_key(#'OTPCertificate'{tbsCertificate=Cert}) ->
public_key(Cert);
public_key(#'OTPTBSCertificate'{subjectPublicKeyInfo=Info}) ->
public_key(Info);
public_key(#'OTPSubjectPublicKeyInfo'{subjectPublicKey=Key}) ->
pkey(Path) when is_list(Path) ->
{ok, Contents} = file:read_file(Path),
[{'Certificate', DerCert, not_encrypted}] = public_key:pem_decode(Contents),
pkey(public_key:pkix_decode_cert(DerCert, otp));
pkey(#'OTPCertificate'{tbsCertificate=Cert}) ->
pkey(Cert);
pkey(#'OTPTBSCertificate'{subjectPublicKeyInfo=Info}) ->
pkey(Info);
pkey(#'OTPSubjectPublicKeyInfo'{subjectPublicKey=Key}) ->
Key.

0 comments on commit 2559e8b

Please sign in to comment.