sha256 of a juce string?


#1

Hello all,

what would be the best way of SHA256’ing a string in juce? I was doing this:

        ScopedPointer<char> rawChars = new char[input.length()];
        for (long i=0; i<input.length(); i++)
            rawChars[i] = (char)input[i];
        SHA256 sha(rawChars, input.length());

But that seems like a rather bad idea considering some chars might be wide chars!

I looked at various other ways of doing things, but I kind of got stuck… :confused:

  • Bram

#2

Aaggh! No no no no no! 
ScopedPointer can't (and shoudn't!) be used for arrays, because it doesn't call delete[] !!
Always use HeapBlock instead for that kind of thing!

What's wrong with this constructor:

[code]    /** Creates a checksum from a UTF-8 buffer.
        E.g.
        @code SHA256 checksum (myString.toUTF8());
        @endcode
    */
    explicit SHA256 (const CharPointer_UTF8& utf8Text) noexcept;
[/code]
?

Aaggh! No no no no no!
ScopedPointer can’t (and shoudn’t!) be used for arrays, because it doesn’t call delete[] !!
Always use HeapBlock instead for that kind of thing!

What’s wrong with this constructor:

/** Creates a checksum from a UTF-8 buffer. E.g. @code SHA256 checksum (myString.toUTF8()); @endcode */ explicit SHA256 (const CharPointer_UTF8& utf8Text) noexcept;
?


#3

Duely noted :slight_smile:

Absolutely nothing except it induces temporary and localized blindness in redheaded people, aparently…

Thx jules!

  • bram

#4

Btw, Jules, nothing in the docs actually says that ScopedPointer shoudn’t be used for arrays, maybe a useful addition…?

  • bram

#5

Doesn’t it mention that?? Ok, yes, definitely a good idea then!