Benjamin Trott
and 1 contributors


Crypt::OpenPGP::S2k - String-to-key generation


    use Crypt::OpenPGP::S2k;

    # S2k generates an encryption key from a passphrase; in order to
    # understand how large of a key to generate, we need to know which
    # cipher we're using, and what the passphrase is.
    my $cipher = Crypt::OpenPGP::Cipher->new( '...' );
    my $passphrase = 'foo';

    my $s2k = Crypt::OpenPGP::S2k->new( 'Salt_Iter' );
    my $key = $s2k->generate( $passphrase, $cipher->keysize );

    my $serialized = $s2k->save;


Crypt::OpenPGP::S2k implements string-to-key generation for use in generating symmetric cipher keys from standard, arbitrary-length passphrases (like those used to lock secret key files). Since a passphrase can be of any length, and key material must be a very specific length, a method is needed to translate the passphrase into the key. The OpenPGP RFC defines three such methods, each of which this class implements.



Creates a new type of S2k-generator of type $type; valid values for $type are Simple, Salted, and Salt_Iter. These generator types are described in the OpenPGP RFC section 3.6.

Returns the new S2k-generator object.


Given a buffer $buffer of type Crypt::OpenPGP::Buffer, determines the type of S2k from the first octet in the buffer (one of the types listed above in new), then creates a new object of that type and initializes the S2k state from the buffer $buffer. Different initializations occur based on the type of S2k.

Returns the new S2k-generator object.


Serializes the S2k object and returns the serialized form; this form will differ based on the type of S2k.

$s2k->generate($passphrase, $keysize)

Given a passphrase $passphrase, which should be a string of octets of arbitrary length, and a keysize $keysize, generates enough key material to meet the size $keysize, and returns that key material.


Please see the Crypt::OpenPGP manpage for author, copyright, and license information.