PageRenderTime 12ms CodeModel.GetById 7ms app.highlight 3ms RepoModel.GetById 1ms app.codeStats 0ms

/modules/mod_oauth/deps/erlang-oauth/src/oauth_rsa_sha1.erl

https://code.google.com/p/zotonic/
Erlang | 30 lines | 23 code | 7 blank | 0 comment | 0 complexity | 69205bafa1af664c66c7d24df704db35 MD5 | raw file
 1-module(oauth_rsa_sha1).
 2
 3-export([signature/2, verify/3]).
 4
 5-include_lib("public_key/include/public_key.hrl").
 6
 7
 8signature(BaseString, PrivateKeyPath) ->
 9  {ok, [Info]} = public_key:pem_to_der(PrivateKeyPath),
10  {ok, PrivateKey} = public_key:decode_private_key(Info),
11  base64:encode_to_string(public_key:sign(list_to_binary(BaseString), PrivateKey)).
12
13verify(Signature, BaseString, PublicKey) ->
14  public_key:verify_signature(to_binary(BaseString), sha, base64:decode(Signature), public_key(PublicKey)).
15
16to_binary(Term) when is_list(Term) ->
17  list_to_binary(Term);
18to_binary(Term) when is_binary(Term) ->
19  Term.
20
21public_key(Path) when is_list(Path) ->
22  {ok, [{cert, DerCert, not_encrypted}]} = public_key:pem_to_der(Path),
23  {ok, Cert} = public_key:pkix_decode_cert(DerCert, otp),
24  public_key(Cert);
25public_key(#'OTPCertificate'{tbsCertificate=Cert}) ->
26  public_key(Cert);
27public_key(#'OTPTBSCertificate'{subjectPublicKeyInfo=Info}) ->
28  public_key(Info);
29public_key(#'OTPSubjectPublicKeyInfo'{subjectPublicKey=Key}) ->
30  Key.