Skip to content

Instantly share code, notes, and snippets.

@jj1bdx
Created June 12, 2012 04:51
Show Gist options
  • Save jj1bdx/2915108 to your computer and use it in GitHub Desktop.
Save jj1bdx/2915108 to your computer and use it in GitHub Desktop.
Erlang R15B01 SHA256 does not accept bitstrings which do not fit to the byte boundary
2> crypto:sha256(<<255:7>>).
** exception error: bad argument
in function crypto:sha256_nif/1
called as crypto:sha256_nif(<<127:7>>)
in call from crypto:sha256/1 (crypto.erl, line 231)
3> crypto:sha256(<<255:8>>).
<<168,16,10,230,170,25,64,208,182,99,187,49,205,70,97,66,
235,189,189,81,135,19,27,146,217,56,24,152,120,...>>
4> crypto:sha256(<<255:9>>).
** exception error: bad argument
in function crypto:sha256_nif/1
called as crypto:sha256_nif(<<127,1:1>>)
in call from crypto:sha256/1 (crypto.erl, line 231)
5> crypto:sha256(<<65535:15>>).
** exception error: bad argument
in function crypto:sha256_nif/1
called as crypto:sha256_nif(<<255,127:7>>)
in call from crypto:sha256/1 (crypto.erl, line 231)
6> crypto:sha256(<<65535:16>>).
<<202,47,208,15,160,1,25,7,68,193,92,49,118,67,171,9,46,
112,72,206,8,106,36,62,43,233,67,124,137,...>>
@voluntas
Copy link

> internal:sha256(<<65535:15>>).
<<243,98,114,36,189,216,36,25,235,105,90,146,198,97,200,
  191,161,254,134,176,161,225,3,144,94,19,41,119,187,...>>
> internal:sha256(<<65535:16>>).
<<202,47,208,15,160,1,25,7,68,193,92,49,118,67,171,9,46,
  112,72,206,8,106,36,62,43,233,67,124,137,...>>

ライブラリ名は仮ですが、結局時前で実装してしまいました。

@jj1bdx
Copy link
Author

jj1bdx commented Jun 12, 2012

crypto:sha256_nif/1 checks the argument is a binary or not. A binary means a bitstring of 8*n where n is integer and n >= 1 (i.e., exactly fits into the byte boundary). Note that sha256/1 also accept an iolist as the argument. See also enif_inspect_iolist_as_binary() definition in the erl_nif manual.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment