This is a follow up to my previous question on showing unicode string differences. As it turns out the strings appear to be the same, however in one of them the UTF8 flag is on.
SV = PVMG(0x4cca750) at 0x4b3fc90
REFCNT = 1
FLAGS = (PADMY,POK,pPOK,UTF8)
IV = 0
NV = 0
PV = 0x1eda410 "flurbe"\0 [UTF8 "flurbe"]
CUR = 6
LEN = 16
vs
SV = PV(0xf28090) at 0xf4b6a0
REFCNT = 1
FLAGS = (PADMY,POK,pPOK)
PV = 0xf37b90 "flurbe"\0
CUR = 6
LEN = 16
This appears to make a difference between the resulting sha512 hashes when I encrypt the string. Dancer is what is causing the first result to have utf8 as far as I can tell, my other script is simply a command line one, without using dancer in that how can I force it to behave in the same way?
You have an encoding problem, namely the lack thereof. The digest functions operate on octets. You give it characters, which is wrong.
Course of action: encode your characters into octets. UTF-8 is a suitable encoding.
(This is more of a comment than an answer, but it's too big.)
I just ran this program:
and it gave this output:
As you can see,
Devel::Peek::Dump
correctly identifies that the string has been upgraded to UTF-8, but this doesn't affect the SHA-512 hash computed byDigest::SHA
.Edited to add: In a comment above, you mention that your "hashes are random salted". Can these salts include bytes outside the ASCII range? If so, concatenation with a UTF-8-upgraded string can affect their contents. I just ran this modified program:
and it gave this output:
As you can see, the SHA-512 hash of
"$x$y"
depends on whether$x
was UTF-8-upgraded."$x$y"
with a UTF-8-upgraded$x
gives the same SHA-512 hash as does"$x$z"
with a non-UTF-8-upgraded$x
. This is because SHA-512 operates on bytes, not characters, and the concatenation of a UTF-8-upgraded string with a byte-string causes the byte-string to be UTF-8-upgraded.