Chilkat Examples

ChilkatHOME.NET Core C#Android™AutoItCC#C++Chilkat2-PythonCkPythonClassic ASPDataFlexDelphi ActiveXDelphi DLLGoJavaLianjaMono C#Node.jsObjective-CPHP ActiveXPHP ExtensionPerlPowerBuilderPowerShellPureBasicRubySQL ServerSwift 2Swift 3,4,5...TclUnicode CUnicode C++VB.NETVBScriptVisual Basic 6.0Visual FoxProXojo Plugin

Android™ Examples

Web API Categories

ASN.1
AWS KMS
AWS Misc
Amazon EC2
Amazon Glacier
Amazon S3
Amazon S3 (new)
Amazon SES
Amazon SNS
Amazon SQS
Async
Azure Cloud Storage
Azure Key Vault
Azure Service Bus
Azure Table Service
Base64
Bounced Email
Box
CAdES
CSR
CSV
Certificates
Cloud Signature CSC
Code Signing
Compression
DKIM / DomainKey
DNS
DSA
Diffie-Hellman
Digital Signatures
Dropbox
Dynamics CRM
EBICS
ECC
Ed25519
Email Object
Encryption
FTP
FileAccess
Firebase
GMail REST API
GMail SMTP/IMAP/POP
Geolocation
Google APIs
Google Calendar
Google Cloud SQL
Google Cloud Storage
Google Drive
Google Photos
Google Sheets
Google Tasks
Gzip
HTML-to-XML/Text
HTTP

HTTP Misc
IMAP
JSON
JSON Web Encryption (JWE)
JSON Web Signatures (JWS)
JSON Web Token (JWT)
Java KeyStore (JKS)
MHT / HTML Email
MIME
MS Storage Providers
Microsoft Graph
Misc
NTLM
OAuth1
OAuth2
OIDC
Office365
OneDrive
OpenSSL
Outlook
Outlook Calendar
Outlook Contact
PDF Signatures
PEM
PFX/P12
PKCS11
POP3
PRNG
REST
REST Misc
RSA
SCP
SCard
SFTP
SMTP
SSH
SSH Key
SSH Tunnel
ScMinidriver
SharePoint
SharePoint Online
Signing in the Cloud
Socket/SSL/TLS
Spider
Stream
Tar Archive
ULID/UUID
Upload
WebSocket
XAdES
XML
XML Digital Signatures
XMP
Zip
curl
uncategorized

 

 

 

(Android™) Hash a Hex String

Demonstrates common pitfalls in hashing a hex string..

Note: This example requires Chilkat v9.5.0.70 or greater.

Chilkat Android™ Downloads

Android™ Java Libraries

Android C/C++ Libraries

// Important: Don't forget to include the call to System.loadLibrary
// as shown at the bottom of this code sample.
package com.test;

import android.app.Activity;
import com.chilkatsoft.*;

import android.widget.TextView;
import android.os.Bundle;

public class SimpleActivity extends Activity {

  private static final String TAG = "Chilkat";

  // Called when the activity is first created.
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // This example assumes the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    CkCrypt2 crypt = new CkCrypt2();

    // We have a hex string to be SHA-256 hashed:  "b08538d832bf"
    // The result we expect to receive is "8a9f04cb1adfbd7f59c5918635f92f6c847e5b15f9828519d2fdbd6ead0918fc"

    String 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:
    crypt.put_Charset("us-ascii");
    // The EncodingMode property defines the binary encoding (hex, base64, etc.) of the hash returned as an encoded string.
    crypt.put_EncodingMode("hex");
    crypt.put_HashAlgorithm("sha256");

    String hashValue = crypt.hashStringENC(strToHash);
    Log.i(TAG, crypt.lastErrorText());
    Log.i(TAG, "hash of 12 us-ascii bytes: " + 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.
    crypt.put_Charset("hex");
    // The EncodingMode and HashAlgorithm remain the same..
    hashValue = crypt.hashStringENC(strToHash);
    Log.i(TAG, crypt.lastErrorText());
    Log.i(TAG, "hash of 6 hex bytes: " + hashValue);

    // The result is 8A9F04CB1ADFBD7F59C5918635F92F6C847E5B15F9828519D2FDBD6EAD0918FC (which equals the hash we were expecting)

  }

  static {
      System.loadLibrary("chilkat");

      // Note: If the incorrect library name is passed to System.loadLibrary,
      // then you will see the following error message at application startup:
      //"The application <your-application-name> has stopped unexpectedly. Please try again."
  }
}

 

© 2000-2024 Chilkat Software, Inc. All Rights Reserved.