Sample code for 30+ languages & platforms
Go

Sign a Byte Array to Create a Detached Signature in a Byte Array

See more Digital Signatures Examples

Signs data contained in a byte array to produce a detached signature (also in a byte array). Also shows how to verify the 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.

    cert := chilkat.NewCert()
    success = cert.LoadPfxFile("qa_data/pfx/cert_test123.pfx","test123")
    if success == false {
        fmt.Println(cert.LastErrorText())
        cert.DisposeCert()
        return
    }

    fac := chilkat.NewFileAccess()

    var fileBytes []byte
    fileBytes = fac.ReadEntireFile("qa_data/pdf/sample.pdf")
    if fac.LastMethodSuccess() != true {
        fmt.Println(fac.LastErrorText())
        cert.DisposeCert()
        fac.DisposeFileAccess()
        return
    }

    crypt := chilkat.NewCrypt2()

    success = crypt.SetSigningCert(cert)

    // We can sign any type of file.
    // The result is a detached signature (a signature that does not contain the data being signed).

    var sigBytes []byte
    sigBytes = crypt.SignBytes(fileBytes)
    if crypt.LastMethodSuccess() != true {
        fmt.Println(crypt.LastErrorText())
        cert.DisposeCert()
        fac.DisposeFileAccess()
        crypt.DisposeCrypt2()
        return
    }

    success = fac.WriteEntireFile("qa_output/sample.pdf.p7s",sigBytes)
    if fac.LastMethodSuccess() != true {
        fmt.Println(fac.LastErrorText())
        cert.DisposeCert()
        fac.DisposeFileAccess()
        crypt.DisposeCrypt2()
        return
    }

    // We can verify the detached signature like this
    verified := crypt.VerifyBytes(fileBytes,sigBytes)
    if crypt.LastMethodSuccess() != true {
        fmt.Println(crypt.LastErrorText())
        cert.DisposeCert()
        fac.DisposeFileAccess()
        crypt.DisposeCrypt2()
        return
    }

    fmt.Println("Verified = ", verified)

    cert.DisposeCert()
    fac.DisposeFileAccess()
    crypt.DisposeCrypt2()