Deprecation confusion: string/text/xojo.core.textEncondings.convertToData

Hi all,
Revisiting XOJO after many years, so effectively a noob (again).
I’ve been looking to hashing methods for password storage and have come across a great many posts that no longer work - either because everything is deprecated or because of compile errors.

Many methods that rely on text now expect strings, but equivalent methods do not seem to exist. Where deprecated I cannot find what these are replaced with.

Specifically, I was looking at: Tips: Dealing with the Problem of Passwords – Xojo Programming Blog. The sample file in this blog post (http://files.xojo.com/BlogExamples/PasswordHashing.xojo_binary_project) fails to run. It was trivial to fix the error in the ConvertToHex method by adding a .toText to the end of the line being looped. This works but is deprecated and I can’t see what this should be replaced with.

While the code works, almost every line is giving a depracation warning because of:

Core.TextEncoding.ConvertTextToData(value As Text, allowLossy As Boolean = False) As xojo.Core.MemoryBlock

string.toText

Xojo.Crypto

I tried to replace what I could to make get rid of the great number of warnings, but nothing works.
Does anyone have any suggestions on how to modernise:

Dim password As text = PasswordField.Text.ToText
Dim passwordData As Xojo.Core.MemoryBlock
passwordData = Xojo.Core.TextEncoding.UTF8.ConvertTextToData(password)

where .toText and ConvertTextToData are deprecated and with no obvious (to me) replacement? while some can be of string type instead, this doesn’t work with subsequent functions, causing an error.

the line
hashData = Xojo.Crypto.Hash(combinedData, Xojo.Crypto.HashAlgorithms.MD5)
is valid but deprecated.

What seemed the obvious to me to update this would be to change this to
hashData = Crypto.Hash(combinedData, Crypto.HashAlgorithms.MD5)
but this causes an error rather than a warning:

There is more than one method with this name but this does not match any of the available signatures.

yet the ‘signature’ appears correct:
Crypto.Hash(data As MemoryBlock, hashAlgorithm As HashAlgorithms) As MemoryBlock

I cannot find a resource to explain what all the deprecations should be replaced with, and to say this is confusing is an understatement…

Grateful for pointers/suggestions/help,

Stam

I stopped using Text some four years ago when it became clear that many String methods had no Text equivalent. I also threw out the Xojo namespace.

Just stop using Text altogether.

1 Like

Maybe:

Dim password As String = TextField1.Text
Dim passwordData As MemoryBlock
passwordData.StringValue(0, password.Length) = password

I do not see any Warning in Xojo 2025 1.1

For a MD5 based password hash as string it is simple as an one-liner:

hashed = EncodeHex(MD5(pass+salt))

Thanks Tim - but it’s not that I’m choosing to use text. It’s that the vast body of only examples have these methods that do rely on text.

The question is how can I translate this into legal syntax after all these deprecations.

Thanks @Sascha_S, it is showing up as a deprecation warning in my IDE.
And according to Deprecations — Xojo documentation, Xojo.crypto is deprecated.

Thanks @Rick_Araujo - that looks simpler and promising (albeit quite different from literally every example I found online, but then they are all deprecated!). I’ll give it a try - the md5 was really just randomly copying 1 line from the blog code. Is this the same for SHA algorithms?

Thanks - that may do a s straight translation for the older solution I was following, but Rick’s solution seems probably more natural.

This will crash as you never New’d the MemoryBlock.

FYI, String and MemoryBlock have operator_converts for each other, so you can write more simply:

Dim password As String = TextField1.Text
Dim passwordData As MemoryBlock = password

or even more concisely:

Dim passwordData As MemoryBlock = TextField1.Text
1 Like

Yeah, that works best. You were entirely correct that the memory block not being ‘newed’ caused a crash - as far as I can see, new memoryblock(byte_length) is the constructor but that just gets unnecessarily complex.

Your final solution is the simplest fit - I thought that should be possible but it turns out I was modifying code that declared Xojo.core.memoryblock instead of just memoryblock and it choked on that.

I realise this is clear to you guys, but for someone just picking up, or in my case returning after a long hiatus to, XOJO, ingesting the many changes in namespacing and syntax even compared relatively recent posts (although I guess 2019 is not as recent as my perception suggests!) is almost like trying to learn two conflicting languages…

Anyway, thank you for your help, I really appreciate it.
Stam

1 Like

Changes a little bit, but the final hash code will get larger when increasing the SHA-bits

Var pass, salt, hash As String

pass = "password"
salt = "NaCl :)"

hash = EncodeHex(Crypto.Hash(pass+salt, Crypto.HashAlgorithms.SHA1))

2 Likes

This is by far the better way to do this, thank you.
This greatly simplifies the code in the example project in the blog post in my OP, from the original:

Dim password As Text = PasswordField.Text.ToText
Dim passwordData As Xojo.Core.MemoryBlock
passwordData = Xojo.Core.TextEncoding.UTF8.ConvertTextToData(password)

Dim salt As Text = SaltField.Text.ToText
Dim saltData As Xojo.Core.MemoryBlock
saltData = Xojo.Core.TextEncoding.UTF8.ConvertTextToData(salt)

Dim combinedData As New Xojo.Core.MutableMemoryBlock(passwordData)
If Not salt.Empty Then
  combinedData.Append(saltData)
End If

Dim hashData As Xojo.Core.MemoryBlock

Select Case HashPopup.Text
Case "MD5"
  hashData = Xojo.Crypto.Hash(combinedData, Xojo.Crypto.HashAlgorithms.MD5)
Case "SHA1"
  hashData = Xojo.Crypto.Hash(combinedData, Xojo.Crypto.HashAlgorithms.SHA1)
Case "SHA256"
  hashData = Xojo.Crypto.Hash(combinedData, Xojo.Crypto.HashAlgorithms.SHA256)
Case "SHA512"
  hashData = Xojo.Crypto.Hash(combinedData, Xojo.Crypto.HashAlgorithms.SHA512)
Case "PBKDF2"
  hashData = Xojo.Crypto.PBKDF2(saltData, passwordData, 500, 32, Xojo.Crypto.HashAlgorithms.SHA256)
End Select

// Convert hashData to hex for display
HashArea.Text = ConvertToHex(hashData)

which then also required:

function ConvertToHex(mb as Xojo.Core.MemoryBlock) as text
   Dim Hex As Text
   For b As Int8 = 0 To mb.Size - 1
      hex = hex + mb.UInt8Value(b).ToHex(2)
   Next
   Return hex
end ConvertToHex

to the much shorter and more readable (and more importantly valid and not requiring the ConvertToHex function):

var pass as string = PasswordField.Text
var salt as string = SaltField.Text
select case HashPopup.Text
case "MD5"
  HashArea.Text = EncodeHex(MD5(pass+salt))
case "SHA1"
  HashArea.Text = EncodeHex(Crypto.Hash(pass+salt, Crypto.HashAlgorithms.SHA1))
case "SHA256"
  HashArea.Text = EncodeHex(Crypto.Hash(pass+salt, Crypto.HashAlgorithms.SHA256))
case "SHA512"
  HashArea.Text = EncodeHex(Crypto.Hash(pass+salt, Crypto.HashAlgorithms.SHA512))
case "PBKDF2"
  var passwordData as MemoryBlock = pass
  HashArea.Text = EncodeHex(Crypto.PBKDF2(salt, passwordData, 500, 32, Crypto.HashAlgorithms.SHA256))
end Select

And of course for my own purposes, this is now a 1-liner… Can’t thank you enough!
Stam

1 Like