Ruby Examples

ChilkatHOMEASPVisual BasicVB.NETC#Visual C++CMFCDelphiFoxProJavaPerlPHPPythonRubySQL ServerVBScript

Ruby
Examples

Quick Start
Unicode
Byte Array
Bz2
Certificates
CSV
Email
Encryption
FTP
HTML-to-XML
HTTP
IMAP
MHT
MIME
POP3
RSA
S/MIME
Signatures
SFTP
SMTP
Socket / SSL
Spider
SSH
SSH Key
SSH Tunnel
Tar
HTTP Upload
XML
XMP
Zip

More Examples...
String
Email Object
FileAccess
RSS
Atom
Self-Extractor
Service
PPMD
Deflate
Bzip2
DH Key Exchange
DSA

Unreleased...
LZW
Icon

 

 

 

 

 

 

 

Use Certificate and Private Key PEM Files to Create a Digital Signature

Demonstrates how to load a digital certificate from a PEM file, load it's corresponding private key from a PEM file, save the private key to a key container (if necessary), link the certificate to the key container, and use it to create a digital signature.

Download Chilkat Ruby Library

require 'chilkat'

cert = Chilkat::CkCert.new()

#  Load the cert from a PEM file;
cert.LoadFromFile("cert.pem")

pkey = Chilkat::CkPrivateKey.new()

#  Load the private key from an RSA PEM file:
pkey.LoadPemFile("pkey_rsa.pem")

#  If the "chilkat" key container does not already exist,
#  we'll create it and import the private key:
container = Chilkat::CkKeyContainer.new()
needPrivateKeyAccess = true
machineKeyset = false
if (container.OpenContainer("chilkat",needPrivateKeyAccess,machineKeyset) == false)

    #  We need to create the key container and import
    #  the private key:
    success = container.CreateContainer("chilkat",machineKeyset)
    if (success == true)
        isKeyExchangePair = false
        success = container.ImportPrivateKey(pkey,isKeyExchangePair)
        if (success == false)
            print "Failed to import private key into key container" + "\n";
            exit
        end

    else
        print "Failed to create key container" + "\n";
        exit
    end

end

#  At this point, the key container contains the private key.
#  Link the certificate with the key container:
bForSigning = true
success = cert.LinkPrivateKey("chilkat",machineKeyset,bForSigning)
if (success == false)
    print "Failed to link certificate with key container" + "\n";
    exit
end

#  Use Chilkat Crypt (a non-freeware component) to create
#  a digital signature using the certificate w/ private key:
crypt = Chilkat::CkCrypt2.new()

#  Any string argument automatically begins the 30-day trial.
success = crypt.UnlockComponent("30-day trial")
if (success != true)
    print "Crypt component unlock failed" + "\n"
    exit
end

#  Tell the crypt component to use this cert.
crypt.SetSigningCert(cert)

#  We can sign any type of file, creating a .p7s as output:
success = crypt.CreateP7S("license.rtf","license.p7s")
if (success == false)
    print crypt.lastErrorText() + "\n"

    exit
end

print crypt.lastErrorText() + "\n";

#  Verify and restore the original file:
crypt.SetVerifyCert(cert)

success = crypt.VerifyP7S("license.rtf","license.p7s")
if (success == false)
    print crypt.lastErrorText() + "\n"

    exit
end

print "Success!" + "\n"

#  The Chilkat Certificate, Certificate Store, Private Key,
#  Public Key, and Key Container classes / objects are freeware.

#  They are used by and included with the Chilkat Email,
#  Crypt, S/MIME, and other commercial Chilkat components.

 

Need a specific example? Send a request to support@chilkatsoft.com

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