fix: 21.3 compatibility

This commit is contained in:
Roman Hargrave 2022-09-10 11:24:00 -07:00
parent 4007c806c3
commit 1907c26735
No known key found for this signature in database
GPG Key ID: E7679B92360E753A
2 changed files with 14 additions and 9 deletions

View File

@ -39,6 +39,7 @@
-import(crypto, [mac/4]).
-import(uri_string, [compose_query/1,
dissect_query/1]).
-import(misc, [crypto_hmac/3]).
-export([signed_url/7]).
@ -67,7 +68,7 @@ signed_url(Auth, Verb, Service, URL, ExtraHeaders, Time, TTL) ->
% generate and sign the message
StringToSign = string_to_sign(Auth, Time, Service, Verb, UriMap, Headers),
SigningKey = signing_key(Auth, Time, Service),
Signature = encode_hex(mac(hmac, sha256, SigningKey, StringToSign)),
Signature = encode_hex(crypto_hmac(sha256, SigningKey, StringToSign)),
% add signature to the query list and compose URI
SignedQueryString = compose_query([{<<"X-Amz-Signature">>, Signature}|QueryList]),
uri_string:recompose(UriMap#{query => SignedQueryString}).
@ -114,7 +115,7 @@ verb(delete) ->
EncodedData :: binary().
% lowercase binary:encode_hex
encode_hex(Data) ->
str:to_lower(binary:encode_hex(Data)).
str:to_lower(str:to_hexlist(Data)).
-spec iso8601_timestamp_utc(
DateTime :: calendar:datetime()
@ -249,7 +250,7 @@ signing_key(#aws_auth{access_key = AccessKey,
region = Region},
Time,
Service) ->
DateKey = mac(hmac, sha256, <<"AWS4", AccessKey/binary>>, iso8601_date(Time)),
DateRegionKey = mac(hmac, sha256, DateKey, Region),
DateRegionServiceKey = mac(hmac, sha256, DateRegionKey, Service),
mac(hmac, sha256, DateRegionServiceKey, <<"aws4_request">>).
DateKey = crypto_hmac(sha256, <<"AWS4", AccessKey/binary>>, iso8601_date(Time)),
DateRegionKey = crypto_hmac(sha256, DateKey, Region),
DateRegionServiceKey = crypto_hmac(sha256, DateRegionKey, Service),
crypto_hmac(sha256, DateRegionServiceKey, <<"aws4_request">>).

View File

@ -447,12 +447,16 @@ object_url(BucketURL, FileName) ->
FileName :: binary()
) ->
ObjectName :: binary().
% generate a unique-in-time object name
% generate a unique-in-time object name. the name consists of a hash
% derived from the file, node, time, and a random number, a
% forward-slash, and the original filename. this ensures that it does
% not collide with other objects, while the forward slash ensures that
% the client displays only the original file name.
object_name(FileName) ->
MD = crypto:hash_init(sha256),
MDFilename = crypto:hash_update(MD, FileName),
MDNodeName = crypto:hash_update(MDFilename, erlang:atom_to_binary(node())),
MDNodeName = crypto:hash_update(MDFilename, atom_to_list(node())),
MDTime = crypto:hash_update(MDNodeName, <<(os:system_time())>>),
MDRand = crypto:hash_update(MDTime, crypto:strong_rand_bytes(256)),
Hash = crypto:hash_final(MDRand),
<<(binary:encode_hex(Hash))/binary, "/", FileName/binary>>.
<<(str:to_hexlist(Hash))/binary, "/", FileName/binary>>.