开发者

Erlang: Problem with crypo:sha()

This works in my erlang shell:

1> U = default_user_name().
2> crypto:start().
3> crypto:sha(U).
4> crypto:stop().

But when I compile this:

default_pw() ->
    U = default_user_name(),
    crypto:start(),
    crypto:sha(U),
    crypto:stop().

And execute it in the erlang shell, I get this:

1> person:default_pw().

**开发者_如何学JAVA exception error: bad argument

 in function  port_control/3

    called as port_control(crypto_drv04,5,<<"jDoe">>)

 in call from crypto:control/2

Two questions:

1) What am I doing wrong?

2) It seems that I need to start and stop the crypto server. But is there a better place to start and stop it in my application?

Many thanks,

LRP


The error you are getting indicates that the crypto application was not started. An easy way to start it is just to add it as a parameter to erl like so:

$ erl -s crypto

I had a look at the current implementation of crypto and the past release notes for erlang and I came to the following conclusions (someone please correct me if I'm wrong):

  1. Erlang releases <14A require crypto to be started prior to calling any functions because the library used a port driver. This makes sense because I got the error you described with 13B04 after not starting crypto.
  2. Erlang releases >=14A don't require crypto to be started as the implementation now uses NIFs. This makes sense because the current implementation of crypto_server.erl is just a gen_server template, and on 14B03 I don't need to start the application in order to make crypto:sha/1 work.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜