Sample code for 30+ languages & platforms
PowerBuilder

secp256k1 Key Generation and Keccak-256

See more ECC Examples

Starting in v11.0.0, Chilkat supports both secp256k1 key generation and Keccak-256 directly. These algorithms are typically used for Bitcoin and Ethereum.

Chilkat PowerBuilder Downloads

PowerBuilder
integer li_rc
integer li_Success
oleobject loo_Fortuna
string ls_Entropy
oleobject loo_Ecc
oleobject loo_PrivKey
oleobject loo_Sb
oleobject loo_Bd

li_Success = 0

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

// Create a Fortuna PRNG and seed it with system entropy.
// This will be our source of random data for generating the ECDSA private key.
loo_Fortuna = create oleobject
li_rc = loo_Fortuna.ConnectToNewObject("Chilkat.Prng")
if li_rc < 0 then
    destroy loo_Fortuna
    MessageBox("Error","Connecting to COM object failed")
    return
end if
ls_Entropy = loo_Fortuna.GetEntropy(32,"base64")
li_Success = loo_Fortuna.AddEntropy(ls_Entropy,"base64")

loo_Ecc = create oleobject
li_rc = loo_Ecc.ConnectToNewObject("Chilkat.Ecc")

// Generate a random ECDSA private key on the secp256k1 curve.
loo_PrivKey = create oleobject
li_rc = loo_PrivKey.ConnectToNewObject("Chilkat.PrivateKey")

li_Success = loo_Ecc.GenKey("secp256k1",loo_Fortuna,loo_PrivKey)
if li_Success = 0 then
    Write-Debug loo_Ecc.LastErrorText
    destroy loo_Fortuna
    destroy loo_Ecc
    destroy loo_PrivKey
    return
end if

Write-Debug "Successfully generated a sec256k1 key."

// Show how to compute the Keccak-256 hash in a few ways.
loo_Sb = create oleobject
li_rc = loo_Sb.ConnectToNewObject("Chilkat.StringBuilder")

loo_Sb.Append("hello")

Write-Debug "keccak-256: " + loo_Sb.GetHash("keccak-256","hex_lower","utf-8")

// Output:
// keccak-256: 1c8aff950685c2ed4bc3174f3472287b56d9517b9c948127319a09a7a36deac8

// To keccak-256 hash binary data
loo_Bd = create oleobject
li_rc = loo_Bd.ConnectToNewObject("Chilkat.BinData")

loo_Bd.AppendEncoded("00010203040506","hex")

Write-Debug "keccak-256: " + loo_Bd.GetHash("keccak-256","hex_lower")

// Output:
// keccak-256: 801560412425120fa609be232d6fa71c7f64f42aee7977267687dcc0a2f5aa63


destroy loo_Fortuna
destroy loo_Ecc
destroy loo_PrivKey
destroy loo_Sb
destroy loo_Bd