B4J Question (Solved) Get SHA256 hex string from Base64 string

Johan Schoeman

Expert
Licensed User
Longtime User
I have this base 64 string:
B4X:
1KYOvzAp1tmGmGTdHEsODQ61aqVHf3AORGjLDEf58Jw=

It is derived from encoding a SHA256 hex string to a base 64 string. When I use attached sample code to get the sha256 hex string I get the following:

B4X:
D4A60EBF3029D6D9869864DD1C4B0E0D0EB56AA5477F700E4468CB0C47F9F09C

It seems to me as if the decoding should use ISO8859_1 and not UTF8 else I don't get a string that are 256 bits long / a string with 64 hex chars)

Question is - am I doing it correctly (i.e the reverse calc)? The hex string should be 64 chars long as it was a sha256 string that was converted to a base64 string.

B4X:
Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.RootPane.LoadLayout("Layout1")
    MainForm.Show
    

    Dim MyBase64String As String = "1KYOvzAp1tmGmGTdHEsODQ61aqVHf3AORGjLDEf58Jw="
    Dim MyBase64StringAsBytes() As Byte = su.DecodeBase64(MyBase64String)

            
    Log("LENGTE = " & MyBase64StringAsBytes.Length)
    Dim s As String = BytesToString(MyBase64StringAsBytes, 0, MyBase64StringAsBytes.Length, "ISO8859_1")
    Log ("S = " & s)
    
    
    Dim ss As String = StringToHex(s)
    Log(ss)

    
End Sub


Sub StringToHex(StrToHex As String) As String
    Dim bc As ByteConverter
    Return bc.HexFromBytes(StrToHex.GetBytes("ISO8859_1"))
End Sub
 

Attachments

  • decodeV1.zip
    1.7 KB · Views: 261

Johan Schoeman

Expert
Licensed User
Longtime User
Both encodings will return the same result as these are ASCII characters.
If I use UTF8 then I get the following string:
B4X:
D4A60EEFBFBD3029EFBFBDD986EFBFBD64EFBFBD1C4B0E0D0EEFBFBD6AEFBFBD477F700E4468EFBFBD0C47EFBFBDEFBFBD

It does not look like the string when using ISO8859_1 and it is also longer than 64 hex chars.
 
Upvote 0

Johan Schoeman

Expert
Licensed User
Longtime User
My mistake. I missed the DecodeBase64 line.

Your code looks wrong. You are treating non-string bytes as string bytes.

Where does this base64 string come from: 1KYOvzAp1tmGmGTdHEsODQ61aqVHf3AORGjLDEf58Jw ?
It is inside a QR code - my understanding is that the body/message of the QR code was converted to a SHA256 and the SHA256 was converted to a Base64 string.

It goes as follows:
1. Scan the QR Code - then decode scanned string. The decoded string looks as follows:
B4X:
    'Scanned String = {"alg":"sha256","kid":"2****21-adf7-4dc7-96bb-309daf0d6***ukqMmS1CWa9z9l","iss":"ZAF","iat":"2021-10-10T12:11:58.715","exp":"3M","hcert":"[B]eyJpZFR5cGUiOiJSU*****sImlkVmFsdWUiOiI4MjA2MTIwMDE2MDg5IiwiZmlyc3ROYW1lIjoiRmlvbmEiLCJzdXJuYW1lIjoiU2VlYmFuIiwiZGF0ZU9mQmlydGgiOiIxMi1KdW4tMTk4MiIsImltbXVuaXph*****V2ZW50cyI6W3sidmFjY2luZVJlY2VpdmVkIjoiQ09WSUQtMTkgVmFjY2luZSBKYW5zc2VuIiwidmFjY2luZURhdGUiOiIxMi1BdWctMjA*****Byb29mT2ZWYWNjaW5lQ29kZSI6IkNSQ0E0N1VDTkYzQSJ9XSwiZ*****RGF0ZSI6IjEwLUphbi0yMDIyIn0=[/B]","hashalg":"sha256","hash":"[B]1KYOvzAp1tmGmGTdHEsODQ61aqVHf3AORGjLDEf58Jw=[/B]"}
2. The first base64 string (I have added some ***** in place to obfuscate the data), once decoded, looks as follows:
B4X:
{"idType":"RSAID","idValue":"82XXXXXX089","firstName":"Johan","surname":"Schoeman","dateOfBirth":"04-May-1962","immunizationEvents":[{"vaccineReceived":"COVID-19 Vaccine Janssen","vaccineDate":"12-Aug-2021","proofOfVaccineCode":"CRCXXXXXF3A"}],"expiryDate":"10-Jan-2022"}
3. So, it seems to me there is another Base64 code (1KYOvzAp1tmGmGTdHEsODQ61aqVHf3AORGjLDEf58Jw=) which is in all probability the SHA256 that was encoded to base64.
4. With the project that I have attached am trying to get back to the original SHA256 (hex string) from 1KYOvzAp1tmGmGTdHEsODQ61aqVHf3AORGjLDEf58Jw=
5. So, I get this after the reversal: D4A60EBF3029D6D9869864DD1C4B0E0D0EB56AA5477F700E4468CB0C47F9F09C
 
Upvote 0

Johan Schoeman

Expert
Licensed User
Longtime User
My mistake. I missed the DecodeBase64 line.

Your code looks wrong. You are treating non-string bytes as string bytes.

Where does this base64 string come from: 1KYOvzAp1tmGmGTdHEsODQ61aqVHf3AORGjLDEf58Jw ?
Thanks Erel - I have sorted it out.
 
Last edited:
Upvote 0
Top