From: Markus Triska Date: Mon, 18 May 2020 09:21:38 +0000 (+0200) Subject: raise instantiation errors for variable encoding X-Git-Tag: v0.8.123~17^2~4 X-Git-Url: https://git.sagredo.dev/?a=commitdiff_plain;h=23034dd4f54b45c9a707e8f8a6d37dffd0fe7a01;p=scryer-prolog.git raise instantiation errors for variable encoding Reported by notoria in #527. Note that from a declarative perspective, it would indeed be valid to give answers for both available encodings. --- diff --git a/src/prolog/lib/crypto.pl b/src/prolog/lib/crypto.pl index 419c3d4c..bbb80a60 100644 --- a/src/prolog/lib/crypto.pl +++ b/src/prolog/lib/crypto.pl @@ -184,7 +184,7 @@ crypto_random_byte(B) :- '$crypto_random_byte'(B). crypto_data_hash(Data0, Hash, Options0) :- must_be(list, Options0), - option(encoding(Encoding), Options0, utf8), + encoding_options(Encoding, Options0), encoding_bytes(Encoding, Data0, Data), functor_hash_options(algorithm, A, Options0, _), ( hash_algorithm(A) -> true @@ -193,6 +193,9 @@ crypto_data_hash(Data0, Hash, Options0) :- '$crypto_data_hash'(Data, HashBytes, A), hex_bytes(Hash, HashBytes). +encoding_options(Encoding, Options) :- + option(encoding(Encoding), Options, utf8), + must_be(atom, Encoding). default_hash(sha256). @@ -252,7 +255,7 @@ crypto_data_hkdf(Data0, L, Bytes, Options0) :- functor_hash_options(algorithm, Algorithm, Options0, Options), must_be(integer, L), L >= 0, - option(encoding(Encoding), Options, utf8), + encoding_options(Encoding, Options), encoding_bytes(Encoding, Data0, Data), option(salt(SaltBytes), Options, []), must_be_bytes(SaltBytes, crypto_data_hkdf/4), @@ -537,7 +540,7 @@ bytes_base64_([A,B,C|Ls]) --> [W,X,Y,Z], - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ crypto_data_encrypt(PlainText0, Algorithm, Key, IV, CipherText, Options) :- - option(encoding(Encoding), Options, utf8), + encoding_options(Encoding, Options), encoding_bytes(Encoding, PlainText0, PlainText), option(tag(Tag), Options, _), ( nonvar(Tag) -> @@ -586,7 +589,7 @@ crypto_data_decrypt(CipherText0, Algorithm, Key, IV, PlainText, Options) :- must_be_bytes(Key, crypto_data_decrypt/6), must_be_bytes(IV, crypto_data_decrypt/6), must_be(atom, Algorithm), - option(encoding(Encoding), Options, utf8), + encoding_options(Encoding, Options), must_be(list, CipherText0), encoding_bytes(octet, CipherText0, CipherText1), append(CipherText1, Tag, CipherText),