error with StringFromBytes in ByteConverter

isr

Member
Licensed User
Longtime User
I am trying to hash a string variable with the ByteConverter and Encryption libraries and return the hashed value as a string. I found some clues on this thread: http://www.b4x.com/forum/libraries-developers-questions/10273-hex-string.html#post57117.

With this background, I tried code like this:
Code:
Dim Bconv As ByteConverter
Dim datatohash As String
Dim hasheddata(0) As Byte   
Dim md As MessageDigest

datatohash="abcdefg"

hasheddata = Bconv.StringToBytes(datatohash, "UTF8")
hasheddata = md.GetMessageDigest(hasheddata, "SHA-512")
hasheddata=Bconv.StringFromBytes(hasheddata,"UTF8")

However, when I try this code, I get a compiling error: Cannot cast type: {Type=String, Rank=0} to: {Type=Byte,Rank=1} Occurred on line: hasheddata=Bconv.StringFromBytes(hasheddata,"UTF8").

What am I doing wrong?

Thank you for any advice!
 
Thank you for your reply. I am not sure I understand you, though.

GetMessageDigest requires a byte variable as input. But I tried to implement your advice nonetheless with the following code:
Code:
Dim Bconv As ByteConverter
Dim datatohash As String
Dim hasheddata as string    .
Dim md As MessageDigest

datatohash="abcdefg"

hasheddata = md.GetMessageDigest(datatohash, "SHA-512")

It produces the same error as before ("Cannot cast type: {Type=String, Rank=0} to: {Type=Byte,Rank=1}").

Also, is there something special about these operations that prevent using the same variable on the left and right sides of an equation? This is something that BASIC allows and I haven't had troubles with it in B4A, so perhaps the issue is specific to particular operations?

Thanks again!
 
Upvote 0
Also, is there something special about these operations that prevent using the same variable on the left and right sides of an equation? This is something that BASIC allows and I haven't had troubles with it in B4A, so perhaps the issue is specific to particular operations?
No.

I was refering to this line here from your original code:
Code:
hasheddata=Bconv.StringFromBytes(hasheddata,"UTF8")
You are converting the bytes from hasheddata and trying to store the result string back in hasheddata :confused:

Use another variable:
Code:
Dim hashedstring as String
hashedstring=Bconv.StringFromBytes(hasheddata,"UTF8")
 
Upvote 0
Thank you, thedesolatesoul.

It does seem as if B4A doesn't allow reading and writing to the same variable for some operations but not others (for example, x=4:x=x+2 works fine). Based on your advice, here's the revised code I used by making sure a variable was only read or written to:
Code:
Dim Bconv As ByteConverter
Dim datatohash As String
Dim bytedata(0) as Byte
Dim hasheddata(0) As Byte 
Dim stringdata As String   
Dim md As MessageDigest

datatohash="abcdefg"

bytedata = Bconv.StringToBytes(datatohash, "UTF8")
hasheddata = md.GetMessageDigest(bytedata, "SHA-512")
stringdata=Bconv.StringFromBytes(hasheddata,"UTF8")
Log (stringdata)

While this code doesn't produce a compiling error, it also doesn't seem to produce a hash as string. When I run this code, I get funny, non-string characters.

I was able to run similar code as my original (and as in the thread http://www.b4x.com/forum/libraries-developers-questions/10273-hex-string.html#post57117) to get a hexadecimal hash (without errors), but it seems that the SQLite database won't accept hexadecimal. That's why I'm trying to get a string version of a hash.

How does one create a hash that SQLite will accept?
 
Upvote 0
It does seem as if B4A doesn't allow reading and writing to the same variable for some operations but not others (for example, x=4:x=x+2 works fine).
isr, B4A does allow reading and writing to the same variable in the same line. This should never be a problem. However, what I was trying to explain to you was that you were writing a 'String' value into a 'Byte' type variable.


I was able to run similar code as my original (and as in the thread http://www.b4x.com/forum/libraries-developers-questions/10273-hex-string.html#post57117) to get a hexadecimal hash (without errors), but it seems that the SQLite database won't accept hexadecimal. That's why I'm trying to get a string version of a hash.

How does one create a hash that SQLite will accept?
I have no experience with SQLite. I would have thought you just need to convert your hexadecimal to a string and save it in the DB. Maybe someone else can comment on that.
 
Upvote 0
desolatesoul,

I understand now, thank you.

The strange thing is that the error-free code doesn't seem to produce a string, although I would think it should.

Does anyone have any insight to this problem or another solution for getting a hash into SQLite?
 
Upvote 0
I worked more on this and can report some resolution. For those looking to hash something into a string, here is the code that worked for me:
Code:
Dim Bconv As ByteConverter
Dim datatohash As String
Dim bytedata(0) as Byte
Dim hasheddata(0) As Byte    
Dim stringdata As String 
Dim md As MessageDigest

datatohash="abcdefg"

bytedata = Bconv.StringToBytes(datatohash, "UTF8")
hasheddata = md.GetMessageDigest(bytedata, "SHA-512")
stringdata=Bconv.HexFromBytes(hasheddata)

This produces a hexadecimal value that can be treated as a string, and SQLite will accept it as a string (what I said before was wrong; I had an error in my query).

I was still not able to get an actual string value from the StringFromBytes function.
 
Upvote 0
Back
Top