0

Logic which is working in Abinitio platform. Lets take sample value as “123456789”, for which we need to generate SHA256 and convert into unsigned integer(7). Expected result - 40876285344408085

m_eval 'hash_SHA256("123456789")'
[void 0x15 0xe2 0xb0 0xd3 0xc3 0x38 0x91 0xeb 0xb0 0xf1 0xef 0x60 0x9e 0xc4 0x19 0x42 0x0c 0x20 0xe3 0x20 0xce 0x94 0xc6 0x5f 0xbc 0x8c 0x33 0x12 0x44 0x8e 0xb2 0x25]

m_eval 'string_to_hex(hash_SHA256("123456789"))' "15E2B0D3C33891EBB0F1EF609EC419420C20E320CE94C65FBC8C3312448EB225"

m_eval '(unsigned integer(7)) reinterpret(hash_SHA256("123456789"))'

40876285344408085

Scala Approach

println("Input Value : "+shaVal)

val shaCode="SHA-256"
val utf="UTF-8"

val digest = MessageDigest.getInstance(shaCode)
  println("digest SHA-256 : "+digest)

val InpStr = StringUtils.stripStart(shaVal,"0")
  println("InpStr : "+InpStr)

val hashUTF = digest.digest(InpStr.getBytes(utf))
  println("hashUTF(UTF-8) : "+hashUTF.mkString(" "))

val hashBigInt= new BigInteger(1, digest.digest(InpStr.getBytes("UTF-8")))
  println("hashBigInt : "+hashBigInt)

val HashKeyRes = String.format("%032x", hashBigInt)
  println("HashKeyRes : "+HashKeyRes) 

Console Output

hashUTF(UTF-16) : 21 -30 -80 -45 -61 56 -111 -21 -80 -15 -17 96 -98 -60 25 66 12 32 -29 32 -50 -108 -58 95 -68 -116 51 18 68 -114 -78 37
hashBigInt : 9899097673353459346982371669967256498000649460813128595014811958380719944229
HashKeyRes : 15e2b0d3c33891ebb0f1ef609ec419420c20e320ce94c65fbc8c3312448eb225
fromBase : 16
toBase : 10

So the hash key generated matches with the value, which is HEX format (Base16). But the expected output should be in(Base10) Unsigned Integer (7) as 40876285344408085

Alyona Yavorska
  • 569
  • 2
  • 14
  • 20
Smahato
  • 1
  • 1
  • I have a suspicion that your expectations are incorrect. Specifically, if you convert a 32-digits hex number into decimal _properly_, there's no way it'll end up as a 17-digit decimal. So, there seems to be some truncation happening in the `(unsigned integer(7)) reinterpret(hash_SHA256("123456789"))`, but in scala code you're not truncating anything. So, either you need to figure out how exactly it is truncated in the "working" version, or maybe your "working" version has an error and should not truncate? – J0HN Jun 09 '20 at 01:51
  • Input is a 9 digit no. SHA produced array[byte] which has 32 elements. I have got 3 possible formats available, you can try converting anyone into Unsigned Integer(7) from any of the below formats. hashUTF(UTF-16) :Array [bytes] = [ 21 -30 -80 -45 -61 56 -111 -21 -80 -15 -17 96 -98 -60 25 66 12 32 -29 32 -50 -108 -58 95 -68 -116 51 18 68 -114 -78 37 ] hashBigInt : BigInteger = 9899097673353459346982371669967256498000649460813128595014811958380719944229 HashKeyRes : Hex(Base16)= 15e2b0d3c33891ebb0f1ef609ec419420c20e320ce94c65fbc8c3312448eb225 I was trying to convert from hex to Int – Smahato Jun 10 '20 at 21:13
  • In Abinitio hash_SHA256("123456789") when casted as unsigned integer(7) has produced result as "40876285344408085" – Smahato Jun 10 '20 at 21:17
  • I understand what you're trying to achieve, but you're essentially asking "how could I truncate the 32 hex numbers into 17 decimal numbers the same way abinitio is doing it". There's no obvious error or visible pattern (at least to me :)), so it's unlikely someone (including me) would invest enough time to reverse-engineer abinitio truncation - maybe try asking their support would work better and produce more authoritative answer? – J0HN Jun 11 '20 at 02:48
  • @J0HN - So you mean any value if converted across different platforms will generate different results.? – Smahato Jun 11 '20 at 16:50
  • No, I mean that reinterpret cast of 32 digit hex into unsigned integer(7) looses precision. How exactly it loses precision depends on the implementation. Implementation, unless it follows some standard, can (and probably will) vary between platforms. So, I would suggest avoiding something that depends on the implementation and sticking with something you can control - e.g. bit shift, taking N least/most significant bytes, etc. With your current approach the only option is to reverse-engineer the truncation that happens in the `(unsigned integer(7)) reinterpret(hash_SHA256("123456789"))` – J0HN Jun 12 '20 at 01:23

0 Answers0