diff options
author | Doug Hogan <[email protected]> | 2019-01-03 22:14:12 -0800 |
---|---|---|
committer | Doug Hogan <[email protected]> | 2019-01-08 01:11:57 -0800 |
commit | 35d9cb839ea0ef764d266212d7f3419c8750b59d (patch) | |
tree | 1525a0080d4c09035e50b8434f79033103621c6a /lib/crypto/c_src/hash.c | |
parent | 0ba6e8338bb1895075f20d8feaa6ad2cb8559ce4 (diff) | |
download | otp-35d9cb839ea0ef764d266212d7f3419c8750b59d.tar.gz otp-35d9cb839ea0ef764d266212d7f3419c8750b59d.tar.bz2 otp-35d9cb839ea0ef764d266212d7f3419c8750b59d.zip |
Revamp hash_nif()
Diffstat (limited to 'lib/crypto/c_src/hash.c')
-rw-r--r-- | lib/crypto/c_src/hash.c | 36 |
1 files changed, 22 insertions, 14 deletions
diff --git a/lib/crypto/c_src/hash.c b/lib/crypto/c_src/hash.c index c8835296ea..34e6db6b66 100644 --- a/lib/crypto/c_src/hash.c +++ b/lib/crypto/c_src/hash.c @@ -64,28 +64,36 @@ ERL_NIF_TERM hash_nif(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]) ErlNifBinary data; ERL_NIF_TERM ret; unsigned ret_size; + unsigned char *outp; - digp = get_digest_type(argv[0]); - if (!digp || - !enif_inspect_iolist_as_binary(env, argv[1], &data)) { - return enif_make_badarg(env); - } - md = digp->md.p; - if (!md) { - return atom_notsup; - } + if (argc != 2) + goto bad_arg; + if ((digp = get_digest_type(argv[0])) == NULL) + goto bad_arg; + if (!enif_inspect_iolist_as_binary(env, argv[1], &data)) + goto bad_arg; + + if ((md = digp->md.p) == NULL) + goto err; ret_size = (unsigned)EVP_MD_size(md); ASSERT(0 < ret_size && ret_size <= EVP_MAX_MD_SIZE); - if (!EVP_Digest(data.data, data.size, - enif_make_new_binary(env, ret_size, &ret), &ret_size, - md, NULL)) { - return atom_notsup; - } + + if ((outp = enif_make_new_binary(env, ret_size, &ret)) == NULL) + goto err; + if (EVP_Digest(data.data, data.size, outp, &ret_size, md, NULL) != 1) + goto err; + ASSERT(ret_size == (unsigned)EVP_MD_size(md)); CONSUME_REDS(env, data); return ret; + + bad_arg: + return enif_make_badarg(env); + + err: + return atom_notsup; } #if OPENSSL_VERSION_NUMBER >= PACKED_OPENSSL_VERSION_PLAIN(1,0,0) |