Skip to content

Commit f116f6a

Browse files
authored
Fix hex_repo.fingerprint (#177)
1 parent d8860b9 commit f116f6a

3 files changed

Lines changed: 8 additions & 4 deletions

File tree

src/hex_core.app.src

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
{description, "Reference implementation of Hex specifications"},
33
{vsn, "0.15.0"},
44
{registered, []},
5-
{applications, [kernel, stdlib, inets, ssl]},
5+
{applications, [kernel, stdlib, inets, ssl, ssh]},
66
{licenses, ["Apache-2.0"]},
77
{include_paths, ["CHANGELOG.md"]},
88
{exclude_paths, ["src/safe_erl_term.erl"]},

src/hex_repo.erl

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -240,18 +240,20 @@ fingerprint(PublicKeyPem) when is_binary(PublicKeyPem) ->
240240
%% true
241241
%% '''
242242
%% @end
243-
-spec fingerprint_equal(binary(), string()) -> boolean().
243+
-spec fingerprint_equal(binary(), iodata()) -> boolean().
244244
fingerprint_equal(PublicKeyPem, ExpectedFingerprint) when is_binary(PublicKeyPem) ->
245245
ActualFingerprint = fingerprint(PublicKeyPem),
246246
constant_time_compare(
247247
list_to_binary(ActualFingerprint),
248-
list_to_binary(ExpectedFingerprint)
248+
iolist_to_binary(ExpectedFingerprint)
249249
).
250250

251251
%% @private
252252
%% Constant-time comparison to prevent timing attacks.
253253
%% Uses crypto:hash_equals/2 on OTP 25+, falls back to manual comparison on older versions.
254254
-if(?OTP_RELEASE >= 25).
255+
constant_time_compare(A, B) when byte_size(A) =/= byte_size(B) ->
256+
false;
255257
constant_time_compare(A, B) ->
256258
crypto:hash_equals(A, B).
257259
-else.

test/hex_repo_SUITE.erl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,9 @@ fingerprint_equal_test(_Config) ->
165165
"-----END PUBLIC KEY-----\n"
166166
>>,
167167
CorrectFingerprint = "SHA256:O1LOYhHFW4kcrblKAxROaDEzLD8bn1seWbe5tq8TRsk",
168-
WrongFingerprint = "SHA256:WrongFingerprint123456789012345678901234567",
168+
WrongFingerprint = <<"SHA256:WrongFingerprint123456789012345678901234567">>,
169+
DifferentLengthFingerprint = "SHA256:TooShort",
169170
?assert(hex_repo:fingerprint_equal(PublicKeyPem, CorrectFingerprint)),
170171
?assertNot(hex_repo:fingerprint_equal(PublicKeyPem, WrongFingerprint)),
172+
?assertNot(hex_repo:fingerprint_equal(PublicKeyPem, DifferentLengthFingerprint)),
171173
ok.

0 commit comments

Comments
 (0)