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

8b1a9953c4611296a827abf8c47804d7
f7ff9e8b7bb2e09b70935a5d785e0cc5d9d0abf0
4149da18aa8bfc2b1e382c6c26556d01a92c261b6436dad5e3be3fcc
185f8db32271fe25f561a6fc938b2e264306ec304eda518007d1764826381969
3519fe5ad2c596efe3e276a6f351b8fc0b03db861782490d45f7598ebd0ab5fd5520ed102f38c4a5ec834e98668035fc
3615f80c9d293ed7402687f94b22d58e529b8cc7916f8fac7fddf7fbd5af4cf777d3d795a7a00a16bf7e7f3fb9561ee9baae480da9fe7a18769e71886b03f315

Test Web
1687466093040.png
 
Last edited:
Upvote 1
Top