Crypto Hash method

Hi there,

I’m trying to hash a text string and to keep the hashed result in a text variable and, optionally, store it in a readable form (Hex, eg).
With the “old framework” I have

Dim tmpText As String = "this is some text" Dim tmpHash As String = Crypto.SHA512(tmpText) Dim hashReadable As String = EncodeHex(tmpHash)

I’m trying to get the same with the new framework.
But at some point I have to convert the memoryblock to text before encoding to Hex

Dim tmpText As Text = "this is some text" Dim tmpClear As MemoryBlock = TextEncoding.UTF8.ConvertTextToData(tmpText) Dim tmpHash As MemoryBlock = SHA512(tmpClear)
Now it seems I have a hash, but I need to convert it to text.

Dim hashText As Text = TextEncoding.UTF8.ConvertDataToText(tmpHash)

This gives a runtime exception: The data could not be converted to text with this encoding.

I think I’m missing something…

[quote=162248:@Olivier Colard]Hi there,

I’m trying to hash a text string and to keep the hashed result in a text variable and, optionally, store it in a readable form (Hex, eg).
With the “old framework” I have

Dim tmpText As String = "this is some text" Dim tmpHash As String = Crypto.SHA512(tmpText) Dim hashReadable As String = EncodeHex(tmpHash)

I’m trying to get the same with the new framework.
But at some point I have to convert the memoryblock to text before encoding to Hex

Dim tmpText As Text = "this is some text" Dim tmpClear As MemoryBlock = TextEncoding.UTF8.ConvertTextToData(tmpText) Dim tmpHash As MemoryBlock = SHA512(tmpClear)
Now it seems I have a hash, but I need to convert it to text.

Dim hashText As Text = TextEncoding.UTF8.ConvertDataToText(tmpHash)

This gives a runtime exception: The data could not be converted to text with this encoding.

I think I’m missing something…[/quote]

You may need to check the validity of the conversion, but I ran into the same problem once, and was able to alleviate the error with :

Dim hashText As Text = TextEncoding.ASCII.ConvertDataToText(tmpHash)

Thanks Michel.

It’s better with ASCII encoding but I’m facing errors sometimes, depending on the hashed data.

I already spent a lot of time trying iOS and I’m not sure I’ll try further with it anymore.
Xojo misses really a lot of functionalities, suffer from a lot of bugs, the documentation for the new framework is often useless and, AFAIK, we’re still not sure to be able to deliver to the app store in Febr.

We need so many Wrappers, DeclareMakers and other GlueKit (thanks to all these people) and still, it’s obviously not enough.
If I have to invest so many time in something I should probably better invest in Obj-C or Swift.

[quote=162958:@Olivier Colard]It’s better with ASCII encoding but I’m facing errors sometimes, depending on the hashed data.

I already spent a lot of time trying iOS and I’m not sure I’ll try further with it anymore.
Xojo misses really a lot of functionalities, suffer from a lot of bugs, the documentation for the new framework is often useless and, AFAIK, we’re still not sure to be able to deliver to the app store in Febr.

We need so many Wrappers, DeclareMakers and other GlueKit (thanks to all these people) and still, it’s obviously not enough.
If I have to invest so many time in something I should probably better invest in Obj-C or Swift.[/quote]

I can assure you 2015R1 will be able to comply to the App Store 64 bit requirements :wink:

I am not sure you are using the right approach to store the hash. It is intrinsically a succession of bytes so it would make more sense to keep it as a memory block, and use binarystream to save it to file. You could also turn it into a hex Text variable by reading bytes from the memoryblock and assemble that as byte.ToHex.

I agree that Xojo iOS is still limited. I got a very severe case of Objective-C allergy, but I kind of like Swift. XCode is undoubtedly extremely complete, with a full compliment of controls and full access to the framework. The main issue is that because of that completeness, and because the framework is accessed raw, programming in Swift is not extremely friendly. One has constantly to refer to the sometimes cryptic Apple Developer Library, and it often feels like writing declares for Xojo.

Also, I have so far had disappointing experiences in the Apple iOS development forum. Seems people there cannot care less about questions, unlike here, and you are quite on your own.

Well. All depends on your requirements. Just like shoes, try on each tool and see what fits best :wink: