Skip to content

Commit

Permalink
Merge pull request #975 from fadushin/doc-crypto-strong_rand_bytes
Browse files Browse the repository at this point in the history
Document `crypto:strong_rand_bytes/1`

These changes are made under both the "Apache 2.0" and the "GNU Lesser General
Public License 2.1 or later" license terms (dual license).

SPDX-License-Identifier: Apache-2.0 OR LGPL-2.1-or-later
  • Loading branch information
bettio committed Dec 9, 2023
2 parents 85eac14 + e425324 commit e715119
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
4 changes: 2 additions & 2 deletions doc/src/programmers-guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -641,10 +641,10 @@ Use `atomvm:random/0` to generate a random unsigned 32-bit integer in the range
%% erlang
RandomInteger = atomvm:random().

Use `atomvm:random_bytes/1` to return a randomly populated binary of a specified size:
Use `crypto:strong_rand_bytes/1` to return a randomly populated binary of a specified size:

%% erlang
RandomBinary = erlang:random_bytes(32).
RandomBinary = crypto:strong_rand_bytes(32).

Use `base64:encode/1` and `base64:decode/1` to encode to and decode from Base64 format. The input value to these functions may be a binary or string. The output value from these functions is an Erlang binary.

Expand Down
14 changes: 13 additions & 1 deletion libs/estdlib/src/crypto.erl
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
-export([
hash/2,
crypto_one_time/4,
crypto_one_time/5
crypto_one_time/5,
strong_rand_bytes/1
]).

-type hash_algorithm() :: md5 | sha | sha224 | sha256 | sha384 | sha512.
Expand Down Expand Up @@ -100,3 +101,14 @@ crypto_one_time(_Cipher, _Key, _Data, _FlagOrOptions) ->
) -> binary().
crypto_one_time(_Cipher, _Key, _IV, _Data, _FlagOrOptions) ->
erlang:nif_error(undefined).

%%-----------------------------------------------------------------------------
%% @param N desired length of cryptographically secure random data
%% @returns Returns Cryptographically secure random data of length `N'
%% @doc Generate N cryptographically secure random octets
%% and return the result in a binary.
%% @end
%%-----------------------------------------------------------------------------
-spec strong_rand_bytes(N :: non_neg_integer()) -> binary().
strong_rand_bytes(_N) ->
erlang:nif_error(undefined).

0 comments on commit e715119

Please sign in to comment.