Skip to content

Commit

Permalink
fix collisions
Browse files Browse the repository at this point in the history
  • Loading branch information
rfourquet committed Oct 7, 2023
1 parent 63fcb20 commit 187f7d1
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions stdlib/Random/src/RNGs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -324,10 +324,14 @@ function hash_seed(str::AbstractString)
SHA.update!(ctx, codeunits(str))
# signature for strings: so far, all hash_seed functions end-up hashing a multiple
# of 4 bytes of data, and add the signature (1 byte) at the end; so hash as many
# 0x05 bytes as necessary to have a total number of hashed bytes equal to 1 mod 4
a = 4 - mod(ncodeunits(str), 4)
SHA.update!(ctx, (0x05, ntuple(Returns(UInt8(a)), a)...))
SHA.digest!(ctx)
# bytes as necessary to have a total number of hashed bytes equal to 0 mod 4 (padding),
# and then hash the signature 0x05; in order for strings of different lengths to have
# different hashes, padding bytes are set equal to the number of padding bytes
pad = 4 - mod(ncodeunits(str), 4)
for _=1:pad
SHA.update!(ctx, (pad % UInt8,))
end
SHA.update!(ctx, (0x05,))
end


Expand Down

0 comments on commit 187f7d1

Please sign in to comment.