1. Alessandro Malaguti
  2. PowerBuilder
  3. Saturday, 6 July 2024 13:52 PM UTC
Hello,
I don't understand why the string results from sha256 encrypted algorithm are different between pb and the sqlserver query

from the PB code non-printable data are returned :
Blob lblb_data
Blob lblb_sha1
string l_res

// here the data to convert
lblb_data = Blob("003806qxgMB&g4pWF$Y20240625160915", EncodingUTF8!) 

CrypterObject lnv_CrypterObject
lnv_CrypterObject = Create CrypterObject

// Encrypt with SHA
lblb_sha1= lnv_CrypterObject.SHA(SHA1!, lblb_data)

// get a string but here non-printable data are returned
l_res = String(lblb_sha1, EncodingUTF8!)
instead if I run the query (sql-server) I see the darta I expect return:
SELECT convert(char(96), HASHBYTES('SHA2_256', '003806qxgMB&g4pWF$Y20240625160915'),2)  ;

in this case the result is readble:
4991A0194A9F80D99E75FBA2D0020A1D17802E77D7CFB8773EEDD633FA9B0F47                                

maybe, do I need to somehow convert the string to pb to get the same result as the query?

thank you

alberto

Alessandro Malaguti Accepted Answer Pending Moderation
  1. Tuesday, 9 July 2024 13:35 PM UTC
  2. PowerBuilder
  3. # 1

yes, works fine:

//
Blob lblb_data
Blob lblb_sha256
String ls_data
string l_res

CoderObject l_CoderObject

lblb_data = Blob("003806qxgMB&g4pWF$Y20240625160915", EncodingUTF8!)

CrypterObject lnv_CrypterObject
lnv_CrypterObject = Create CrypterObject

// Encrypt with SHA
lblb_sha256= lnv_CrypterObject.SHA(SHA256!, lblb_data)  <-- here I had done I had done copy+paste too quickly, fairly is SHA256! not SHA1 to get SHA 256 algorithm

l_res = String(lblb_sha256, EncodingUTF8!)

l_CoderObject = create CoderObject
l_res = l_CoderObject.HexEncode(lblb_sha256)     <--- OK! the result is the same to the sql server query 

thank you!

alberto

Comment
There are no comments made yet.
Arthur Hefti Accepted Answer Pending Moderation
  1. Saturday, 6 July 2024 14:07 PM UTC
  2. PowerBuilder
  3. # 2

Hi Alberto

the string from SQL Server is Hex encoded.

Use:

CoderObject lnv_CoderObject
lnv_CoderObject = CREATE CoderObject

l_res = lnv_CoderObject.HexEncode(lblb_sha1)

Instead of: l_res = String(lblb_sha1, EncodingUTF8!)

In addition you use SHA-256 in SQL Server and simple SHA1 in PB, you need to set SHA256! instead of SHA1!, to get the same values.

Regards
Arthur

Comment
There are no comments made yet.
  • Page :
  • 1


There are no replies made for this question yet.
However, you are not allowed to reply to this question.