Method: PKCS11::Session#encrypt

Defined in:
lib/pkcs11/session.rb

#encrypt(mechanism, key, data = nil) {|PKCS11::Session::Cipher| ... } ⇒ String

Convenience method for the #C_EncryptInit, #C_EncryptUpdate, #C_EncryptFinal call flow.

If no block is given, the single part operation #C_EncryptInit, #C_Encrypt is called. If a block is given, the multi part operation (#C_EncryptInit, #C_EncryptUpdate, #C_EncryptFinal) is used. The given block is called once with a cipher object. There can be any number of PKCS11::Session::Cipher#update calls within the block, each giving the encryption result of this part as String.

Examples:

for using single part operation

iv = "12345678"
cryptogram = session.encrypt( {DES_CBC_PAD: iv}, key, "block 1block 2" )

for using multi part operation

iv = "12345678"
cryptogram = ''
cryptogram << session.encrypt( {DES_CBC_PAD: iv}, key ) do |cipher|
  cryptogram << cipher.update("block 1")
  cryptogram << cipher.update("block 2")
end

Calculating a key check value to a secret key

key_kcv = session.encrypt( :DES3_ECB, key, "\0"*8)

Parameters:

Yields:

Returns:

  • (String)

    the final part of the encryption operation.



317
318
319
320
# File 'lib/pkcs11/session.rb', line 317

def encrypt(mechanism, key, data=nil, &block)
  common_crypt(:C_EncryptInit, :C_EncryptUpdate, :C_EncryptFinal, :C_Encrypt,
               mechanism, key, data, &block)
end