Sample code for 30+ languages & platforms
Go

RSA Hash Binary Data and Sign (and Verify)

See more RSA Examples

Demonstrates how to sign the hash of binary data. Also demonstrates how to verify the RSA signature.

Chilkat Go Downloads

Go
    success := false

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

    // Load an RSA private key for signing.
    privKey := chilkat.NewPrivateKey()
    success = privKey.LoadEncryptedPemFile("qa_data/pem/rsa_passwd.pem","passwd")
    if success == false {
        fmt.Println(privKey.LastErrorText())
        privKey.DisposePrivateKey()
        return
    }

    rsa := chilkat.NewRsa()
    rsa.UsePrivateKey(privKey)

    // We have some binary data (in hex) to sign
    originalData := "0102030405060708090A"
    bdData := chilkat.NewBinData()
    bdData.AppendEncoded(originalData,"hex")

    // Hash (SHA-256) and sign the hash:
    bdSignature := chilkat.NewBinData()
    success = rsa.SignBd(bdData,"sha256",bdSignature)
    if success == false {
        fmt.Println(rsa.LastErrorText())
        privKey.DisposePrivateKey()
        rsa.DisposeRsa()
        bdData.DisposeBinData()
        bdSignature.DisposeBinData()
        return
    }

    // Show the RSA signature in base64
    fmt.Println(*bdSignature.GetEncoded("base64"))

    // ------------------------------------------
    // Get the public key from the private key
    pubKey := chilkat.NewPublicKey()
    privKey.ToPublicKey(pubKey)

    // Verify the signature..
    rsa2 := chilkat.NewRsa()
    rsa2.UsePublicKey(pubKey)

    bVerified := rsa2.VerifyBd(bdData,"sha256",bdSignature)
    fmt.Println("signature verified: ", bVerified)

    privKey.DisposePrivateKey()
    rsa.DisposeRsa()
    bdData.DisposeBinData()
    bdSignature.DisposeBinData()
    pubKey.DisposePublicKey()
    rsa2.DisposeRsa()