B4J Question [SOLVED] How to get a simple sha256 of a string?

Solution
SHA-256 is actually a hashing algorithm, not Encryption. I think i have SHA256 method in my Encryption.bas in Web API 2 optional modules. In future I will rename my module as Hashing.

B4X:
Public Sub SHA256 (str As String) As String
    Dim data() As Byte
    Dim MD As MessageDigest
    Dim BC As ByteConverter

    data = BC.StringToBytes(str, "UTF8")
    data = MD.GetMessageDigest(data, "SHA-256")
    Return BC.HexFromBytes(data).ToLowerCase
End Sub

aeric

Expert
Licensed User
Longtime User
SHA-256 is actually a hashing algorithm, not Encryption. I think i have SHA256 method in my Encryption.bas in Web API 2 optional modules. In future I will rename my module as Hashing.

B4X:
Public Sub SHA256 (str As String) As String
    Dim data() As Byte
    Dim MD As MessageDigest
    Dim BC As ByteConverter

    data = BC.StringToBytes(str, "UTF8")
    data = MD.GetMessageDigest(data, "SHA-256")
    Return BC.HexFromBytes(data).ToLowerCase
End Sub
 
Upvote 1
Solution

Mashiane

Expert
Licensed User
Longtime User
Dim MD As MessageDigest
Which lib in b4j should i reference for this?

Update: Found it:

 
Last edited:
Upvote 0

TILogistic

Expert
Licensed User
Longtime User
just a test of possible encryptions
B4X:
    Log(Encryption("Hello", "MD5"))
    Log(Encryption("Hello", "SHA-1"))
    Log(Encryption("Hello", "SHA-224"))
    Log(Encryption("Hello", "SHA-256"))
    Log(Encryption("Hello", "SHA-384"))
    Log(Encryption("Hello", "SHA-512"))
B4X:
Public Sub Encryption (Text As String, Algorithm As String) As String
    Dim MD As MessageDigest, BC As ByteConverter
    Dim Data() As Byte = MD.GetMessageDigest(BC.StringToBytes(Text, "UTF8"), Algorithm)
    Return BC.HexFromBytes(Data).ToLowerCase
End Sub


Test Web
 
Last edited:
Upvote 1