Description
While reviewing #15931 I noticed some oddities with the conversion from String
to Bytes
in Bcrypt.new
and .hash_secret
.
Lines 73 to 86 in 08940fc
The first oddity is that .clone
call. There's a code comment that claims this is to avoid keeping a mutable reference to the original string. But AFAIK BCrypt
does not mutate the password slice. It exposes it as #password
which might make it available for other things. But it's still a read-only slice. So I don't think there's great danger here. We should be able to avoid the extra allocation.
The API docs for the other overload, which accepts Bytes
explicitly demonstrates its use with "secret".to_slice
.
Also, why does the byte slice include the trailing zero?
At first glance, this seems wrong because trailing zeros are an implementation detail of String
.
I'd expect this to cause compatibility issues with other implementations. But this actually seems to work... Is a trailing zero expected in bcrypt passwords?
Related: #15276