class ICE::Cipher

Public Class Methods

new(p1) click to toggle source
VALUE ice_cipher_initialize(VALUE self, VALUE level)
{
        Check_Type(level, T_FIXNUM);

        ice_cipher_create_key(self, NUM2INT(level), NULL);

        return self;
}

Public Instance Methods

block_size() click to toggle source
VALUE ice_cipher_block_size(VALUE self)
{
        ICE_KEY* key;

        Data_Get_Struct(self, ICE_KEY, key);

        return INT2NUM(ice_key_block_size(key));
}
decrypt(p1) click to toggle source
VALUE ice_cipher_decrypt(VALUE self, VALUE text)
{
        ICE_KEY*       key;
        VALUE          result;
        unsigned int   textLength;
        unsigned char* plainText;
        unsigned char* cipherText;

        // Get the string length
        textLength = RSTRING_LEN(text);

        // Check that the string is divisible by 8
        if (textLength % 8 != 0)
        {
                rb_raise(rb_eArgError, "expected argument to be divisible by 8, but length is %d", textLength);

                return Qnil;
        }

        // Get the contents of the string
        cipherText = (unsigned char*)StringValuePtr(text);

        // Allocate space for the decrypted text
        plainText = (unsigned char*)malloc(textLength);

        // Get the data struct
        Data_Get_Struct(self, ICE_KEY, key);

        // Decrypt the cipher text
        ice_key_decrypt(key, (const unsigned char*)cipherText, plainText);

        if (plainText != NULL)
        {
                result = rb_str_new((const char*)plainText, textLength);
        }
        else
        {
                result = Qnil;
        }

        return result;
}
encrypt(p1) click to toggle source
VALUE ice_cipher_encrypt(VALUE self, VALUE text)
{
        ICE_KEY*       key;
        VALUE          result;
        unsigned int   textLength;
        unsigned char* plainText;
        unsigned char* cipherText;

        // Get the string length
        textLength = RSTRING_LEN(text);

        // Check that the string is divisible by 8
        if (textLength % 8 != 0)
        {
                rb_raise(rb_eArgError, "expected argument to be divisible by 8, but length is %d", textLength);

                return Qnil;
        }

        // Get the contents of the string
        plainText = (unsigned char*)StringValuePtr(text);

        // Allocate space for the cipher text
        cipherText = (unsigned char*)malloc(textLength);

        // Get the data struct
        Data_Get_Struct(self, ICE_KEY, key);

        // Decrypt the cipher text
        ice_key_encrypt(key, (const unsigned char*)plainText, cipherText);

        if (cipherText != NULL)
        {
                result = rb_str_new((const char*)cipherText, textLength);
        }
        else
        {
                result = Qnil;
        }

        return result;
}
key=(p1) click to toggle source
VALUE ice_cipher_set_key(VALUE self, VALUE value)
{
        ICE_KEY* key;

        Check_Type(value, T_STRING);
        Data_Get_Struct(self, ICE_KEY, key);

        if (key != NULL)
        {
                ice_key_set(key, (unsigned char*)StringValuePtr(value));

                return value;
        }

        return Qnil;
}
key_size() click to toggle source
VALUE ice_cipher_key_size(VALUE self)
{
        ICE_KEY* key;

        Data_Get_Struct(self, ICE_KEY, key);

        return INT2NUM(ice_key_key_size(key));
}