Chilkat HOME .NET Core C# Android™ AutoIt C C# C++ Chilkat2-Python CkPython Classic ASP DataFlex Delphi ActiveX Delphi DLL Go Java Lianja Mono C# Node.js Objective-C PHP ActiveX PHP Extension Perl PowerBuilder PowerShell PureBasic Ruby SQL Server Swift 2 Swift 3,4,5... Tcl Unicode C Unicode C++ VB.NET VBScript Visual Basic 6.0 Visual FoxPro Xojo Plugin
(PowerBuilder) Hash a Hex StringDemonstrates common pitfalls in hashing a hex string.. Note: This example requires Chilkat v9.5.0.70 or greater.
integer li_rc oleobject loo_Crypt string ls_StrToHash string ls_HashValue // This example assumes the Chilkat API to have been previously unlocked. // See Global Unlock Sample for sample code. loo_Crypt = create oleobject // Use "Chilkat_9_5_0.Crypt2" for versions of Chilkat < 10.0.0 li_rc = loo_Crypt.ConnectToNewObject("Chilkat.Crypt2") if li_rc < 0 then destroy loo_Crypt MessageBox("Error","Connecting to COM object failed") return end if // We have a hex string to be SHA-256 hashed: "b08538d832bf" // The result we expect to receive is "8a9f04cb1adfbd7f59c5918635f92f6c847e5b15f9828519d2fdbd6ead0918fc" ls_StrToHash = "b08538d832bf" // How do we get this result? // The 1st question to be answered is: What bytes are getting hashed? // There are two possibilities: // 1) Hash 12 bytes, namely the us-ascii values for 'b', '0', '8', '5', .... 'b', 'f' // or // 2) Hash 6 bytes: 0xb0, 0x85, ... 0xbf // // This is how to hash the 12 us-ascii byte values: // The Charset property defines the byte representation of the string passed to the hash algorithm: loo_Crypt.Charset = "us-ascii" // The EncodingMode property defines the binary encoding (hex, base64, etc.) of the hash returned as an encoded string. loo_Crypt.EncodingMode = "hex" loo_Crypt.HashAlgorithm = "sha256" ls_HashValue = loo_Crypt.HashStringENC(ls_StrToHash) Write-Debug loo_Crypt.LastErrorText Write-Debug "hash of 12 us-ascii bytes: " + ls_HashValue // The result is: 327F2B33A0F0580D09840B0D7CEE54514CA33E9A (not what we were hoping). // ------------------------------------------- // This is how to hash the 6 bytes // // "hex" is not an actual character encoding. It's a special value to be used to tell Chilkat to hex decode // the string and pass the decoded bytes to the hash algorithm... // Note: This example requires Chilkat v9.5.0.70 or greater for the "hex" Charset to work properly. loo_Crypt.Charset = "hex" // The EncodingMode and HashAlgorithm remain the same.. ls_HashValue = loo_Crypt.HashStringENC(ls_StrToHash) Write-Debug loo_Crypt.LastErrorText Write-Debug "hash of 6 hex bytes: " + ls_HashValue // The result is 8A9F04CB1ADFBD7F59C5918635F92F6C847E5B15F9828519D2FDBD6EAD0918FC (which equals the hash we were expecting) destroy loo_Crypt |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.