centralize reasoning about encoding
This commit is contained in:
@@ -184,8 +184,7 @@ crypto_random_byte(B) :- '$crypto_random_byte'(B).
|
|||||||
|
|
||||||
crypto_data_hash(Data0, Hash, Options0) :-
|
crypto_data_hash(Data0, Hash, Options0) :-
|
||||||
must_be(list, Options0),
|
must_be(list, Options0),
|
||||||
encoding_options(Encoding, Options0),
|
options_data_bytes(Options0, Data0, Data),
|
||||||
encoding_bytes(Encoding, Data0, Data),
|
|
||||||
functor_hash_options(algorithm, A, Options0, _),
|
functor_hash_options(algorithm, A, Options0, _),
|
||||||
( hash_algorithm(A) -> true
|
( hash_algorithm(A) -> true
|
||||||
; domain_error(hash_algorithm, A, crypto_data_hash/3)
|
; domain_error(hash_algorithm, A, crypto_data_hash/3)
|
||||||
@@ -193,9 +192,10 @@ crypto_data_hash(Data0, Hash, Options0) :-
|
|||||||
'$crypto_data_hash'(Data, HashBytes, A),
|
'$crypto_data_hash'(Data, HashBytes, A),
|
||||||
hex_bytes(Hash, HashBytes).
|
hex_bytes(Hash, HashBytes).
|
||||||
|
|
||||||
encoding_options(Encoding, Options) :-
|
options_data_bytes(Options, Data, Bytes) :-
|
||||||
option(encoding(Encoding), Options, utf8),
|
option(encoding(Encoding), Options, utf8),
|
||||||
must_be(atom, Encoding).
|
must_be(atom, Encoding),
|
||||||
|
encoding_bytes(Encoding, Data, Bytes).
|
||||||
|
|
||||||
default_hash(sha256).
|
default_hash(sha256).
|
||||||
|
|
||||||
@@ -255,8 +255,7 @@ crypto_data_hkdf(Data0, L, Bytes, Options0) :-
|
|||||||
functor_hash_options(algorithm, Algorithm, Options0, Options),
|
functor_hash_options(algorithm, Algorithm, Options0, Options),
|
||||||
must_be(integer, L),
|
must_be(integer, L),
|
||||||
L >= 0,
|
L >= 0,
|
||||||
encoding_options(Encoding, Options),
|
options_data_bytes(Options, Data0, Data),
|
||||||
encoding_bytes(Encoding, Data0, Data),
|
|
||||||
option(salt(SaltBytes), Options, []),
|
option(salt(SaltBytes), Options, []),
|
||||||
must_be_bytes(SaltBytes, crypto_data_hkdf/4),
|
must_be_bytes(SaltBytes, crypto_data_hkdf/4),
|
||||||
option(info(Info0), Options, []),
|
option(info(Info0), Options, []),
|
||||||
@@ -540,8 +539,7 @@ bytes_base64_([A,B,C|Ls]) --> [W,X,Y,Z],
|
|||||||
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
|
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
|
||||||
|
|
||||||
crypto_data_encrypt(PlainText0, Algorithm, Key, IV, CipherText, Options) :-
|
crypto_data_encrypt(PlainText0, Algorithm, Key, IV, CipherText, Options) :-
|
||||||
encoding_options(Encoding, Options),
|
options_data_bytes(Options, PlainText0, PlainText),
|
||||||
encoding_bytes(Encoding, PlainText0, PlainText),
|
|
||||||
option(tag(Tag), Options, _),
|
option(tag(Tag), Options, _),
|
||||||
( nonvar(Tag) ->
|
( nonvar(Tag) ->
|
||||||
must_be_bytes(Tag, crypto_data_encrypt/6)
|
must_be_bytes(Tag, crypto_data_encrypt/6)
|
||||||
|
|||||||
Reference in New Issue
Block a user