Class: OpenSSL::Cipher::Cipher
- Inherits:
-
Object
- Object
- OpenSSL::Cipher::Cipher
- Defined in:
- lib/openssl/cipher.rb,
ossl_cipher.c
Instance Method Summary collapse
- #<<(data) ⇒ Object
- #block_size ⇒ Object
- #decrypt(*args) ⇒ Object
- #encrypt(*args) ⇒ Object
- #final ⇒ Object
- #initialize(str) ⇒ Object constructor
- #iv=(iv) ⇒ Object
- #iv_len ⇒ Object
- #key=(key) ⇒ Object
- #key_len ⇒ Object
- #key_len=(key_length) ⇒ Object
- #name ⇒ Object
- #padding=(padding) ⇒ Object
- #pkcs5_keyivgen(*args) ⇒ Object
- #random_iv ⇒ Object
- #random_key ⇒ Object
- #reset ⇒ Object
- #update(data) ⇒ Object
Constructor Details
#initialize(str) ⇒ Object
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'ossl_cipher.c', line 87
static VALUE
ossl_cipher_initialize(VALUE self, VALUE str)
{
EVP_CIPHER_CTX *ctx;
const EVP_CIPHER *cipher;
char *name;
name = StringValuePtr(str);
GetCipher(self, ctx);
if (!(cipher = EVP_get_cipherbyname(name))) {
ossl_raise(rb_eRuntimeError, "unsupported cipher algorithm (%s)", name);
}
if (EVP_CipherInit_ex(ctx, cipher, NULL, NULL, NULL, -1) != 1)
ossl_raise(eCipherError, NULL);
return self;
}
|
Instance Method Details
#<<(data) ⇒ Object
264 265 266 267 268 269 270 271 272 |
# File 'ossl_cipher.c', line 264
static VALUE
ossl_cipher_update_deprecated(VALUE self, VALUE data)
{
char *cname;
cname = rb_class2name(rb_obj_class(self));
rb_warning("%s#<< is deprecated; use %s#update instead", cname, cname);
return ossl_cipher_update(self, data);
}
|
#block_size ⇒ Object
#decrypt(*args) ⇒ Object
205 206 207 208 209 |
# File 'ossl_cipher.c', line 205
static VALUE
ossl_cipher_decrypt(int argc, VALUE *argv, VALUE self)
{
return ossl_cipher_init(argc, argv, self, 0);
}
|
#encrypt(*args) ⇒ Object
199 200 201 202 203 |
# File 'ossl_cipher.c', line 199
static VALUE
ossl_cipher_encrypt(int argc, VALUE *argv, VALUE self)
{
return ossl_cipher_init(argc, argv, self, 1);
}
|
#final ⇒ Object
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 |
# File 'ossl_cipher.c', line 274
static VALUE
ossl_cipher_final(VALUE self)
{
EVP_CIPHER_CTX *ctx;
int out_len;
VALUE str;
GetCipher(self, ctx);
str = rb_str_new(0, EVP_CIPHER_CTX_block_size(ctx));
if (!EVP_CipherFinal_ex(ctx, RSTRING(str)->ptr, &out_len))
ossl_raise(eCipherError, NULL);
assert(out_len <= RSTRING(str)->len);
RSTRING(str)->len = out_len;
RSTRING(str)->ptr[out_len] = 0;
return str;
}
|
#iv=(iv) ⇒ Object
319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 |
# File 'ossl_cipher.c', line 319
static VALUE
ossl_cipher_set_iv(VALUE self, VALUE iv)
{
EVP_CIPHER_CTX *ctx;
StringValue(iv);
GetCipher(self, ctx);
if (RSTRING(iv)->len < EVP_CIPHER_CTX_iv_length(ctx))
ossl_raise(eCipherError, "iv length too short");
if (EVP_CipherInit_ex(ctx, NULL, NULL, NULL, RSTRING(iv)->ptr, -1) != 1)
ossl_raise(eCipherError, NULL);
return iv;
}
|
#iv_len ⇒ Object
#key=(key) ⇒ Object
302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 |
# File 'ossl_cipher.c', line 302
static VALUE
ossl_cipher_set_key(VALUE self, VALUE key)
{
EVP_CIPHER_CTX *ctx;
StringValue(key);
GetCipher(self, ctx);
if (RSTRING(key)->len < EVP_CIPHER_CTX_key_length(ctx))
ossl_raise(eCipherError, "key length too short");
if (EVP_CipherInit_ex(ctx, NULL, NULL, RSTRING(key)->ptr, NULL, -1) != 1)
ossl_raise(eCipherError, NULL);
return key;
}
|
#key_len ⇒ Object
#key_len=(key_length) ⇒ Object
336 337 338 339 340 341 342 343 344 345 346 347 |
# File 'ossl_cipher.c', line 336
static VALUE
ossl_cipher_set_key_length(VALUE self, VALUE key_length)
{
EVP_CIPHER_CTX *ctx;
int len = NUM2INT(key_length);
GetCipher(self, ctx);
if (EVP_CIPHER_CTX_set_key_length(ctx, len) != 1)
ossl_raise(eCipherError, NULL);
return key_length;
}
|
#name ⇒ Object
292 293 294 295 296 297 298 299 300 |
# File 'ossl_cipher.c', line 292
static VALUE
ossl_cipher_name(VALUE self)
{
EVP_CIPHER_CTX *ctx;
GetCipher(self, ctx);
return rb_str_new2(EVP_CIPHER_name(EVP_CIPHER_CTX_cipher(ctx)));
}
|
#padding=(padding) ⇒ Object
349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 |
# File 'ossl_cipher.c', line 349
static VALUE
ossl_cipher_set_padding(VALUE self, VALUE padding)
{
#if defined(HAVE_EVP_CIPHER_CTX_SET_PADDING)
EVP_CIPHER_CTX *ctx;
int pad = NUM2INT(padding);
GetCipher(self, ctx);
if (EVP_CIPHER_CTX_set_padding(ctx, pad) != 1)
ossl_raise(eCipherError, NULL);
#else
rb_notimplement();
#endif
return padding;
}
|
#pkcs5_keyivgen(*args) ⇒ Object
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 |
# File 'ossl_cipher.c', line 211
static VALUE
ossl_cipher_pkcs5_keyivgen(int argc, VALUE *argv, VALUE self)
{
EVP_CIPHER_CTX *ctx;
const EVP_MD *digest;
VALUE vpass, vsalt, viter, vdigest;
unsigned char key[EVP_MAX_KEY_LENGTH], iv[EVP_MAX_IV_LENGTH], *salt = NULL;
int iter;
rb_scan_args(argc, argv, "13", &vpass, &vsalt, &viter, &vdigest);
StringValue(vpass);
if(!NIL_P(vsalt)){
StringValue(vsalt);
if(RSTRING(vsalt)->len != PKCS5_SALT_LEN)
rb_raise(eCipherError, "salt must be an 8-octet string");
salt = RSTRING(vsalt)->ptr;
}
iter = NIL_P(viter) ? 2048 : NUM2INT(viter);
digest = NIL_P(vdigest) ? EVP_md5() : GetDigestPtr(vdigest);
GetCipher(self, ctx);
EVP_BytesToKey(EVP_CIPHER_CTX_cipher(ctx), digest, salt,
RSTRING(vpass)->ptr, RSTRING(vpass)->len, iter, key, iv);
if (EVP_CipherInit_ex(ctx, NULL, NULL, key, iv, -1) != 1)
ossl_raise(eCipherError, NULL);
OPENSSL_cleanse(key, sizeof key);
OPENSSL_cleanse(iv, sizeof iv);
return Qnil;
}
|
#random_iv ⇒ Object
51 52 53 54 55 |
# File 'lib/openssl/cipher.rb', line 51 def random_iv str = OpenSSL::Random.random_bytes(self.iv_len) self.iv = str return str end |
#random_key ⇒ Object
45 46 47 48 49 |
# File 'lib/openssl/cipher.rb', line 45 def random_key str = OpenSSL::Random.random_bytes(self.key_len) self.key = str return str end |
#reset ⇒ Object
144 145 146 147 148 149 150 151 152 153 154 |
# File 'ossl_cipher.c', line 144
static VALUE
ossl_cipher_reset(VALUE self)
{
EVP_CIPHER_CTX *ctx;
GetCipher(self, ctx);
if (EVP_CipherInit_ex(ctx, NULL, NULL, NULL, NULL, -1) != 1)
ossl_raise(eCipherError, NULL);
return self;
}
|
#update(data) ⇒ Object
241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 |
# File 'ossl_cipher.c', line 241
static VALUE
ossl_cipher_update(VALUE self, VALUE data)
{
EVP_CIPHER_CTX *ctx;
char *in;
int in_len, out_len;
VALUE str;
StringValue(data);
in = RSTRING(data)->ptr;
if ((in_len = RSTRING(data)->len) == 0)
rb_raise(rb_eArgError, "data must not be empty");
GetCipher(self, ctx);
str = rb_str_new(0, in_len+EVP_CIPHER_CTX_block_size(ctx));
if (!EVP_CipherUpdate(ctx, RSTRING(str)->ptr, &out_len, in, in_len))
ossl_raise(eCipherError, NULL);
assert(out_len < RSTRING(str)->len);
RSTRING(str)->len = out_len;
RSTRING(str)->ptr[out_len] = 0;
return str;
}
|