diff options
author | Technorama team <oss-ruby@technorama.net> | 2004-02-18 23:53:20 +0000 |
---|---|---|
committer | Technorama team <oss-ruby@technorama.net> | 2004-02-18 23:53:20 +0000 |
commit | 69ffa78d84721355781d2362f568b361930d723a (patch) | |
tree | 4c1d963d06f9ddcf91ed69ed1269d498510386a2 | |
parent | 47eaa395611d58bed369d936f8bfe8938a18ce63 (diff) | |
download | ruby-openssl-history-69ffa78d84721355781d2362f568b361930d723a.tar.gz |
add pkcs5_v15_password
-rw-r--r-- | ossl_cipher.c | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/ossl_cipher.c b/ossl_cipher.c index 3d63a2a..50ae909 100644 --- a/ossl_cipher.c +++ b/ossl_cipher.c @@ -212,6 +212,36 @@ ossl_cipher_decrypt(int argc, VALUE *argv, VALUE self) return self; } +static VALUE +ossl_cipher_pkcs5_v15_password(int argc, VALUE *argv, VALUE self) +{ + EVP_CIPHER_CTX *ctx; + unsigned char iv[EVP_MAX_IV_LENGTH], key[EVP_MAX_KEY_LENGTH]; + VALUE digest, rounds, pass, salt; + const EVP_MD *md; + + GetCipher(self, ctx); + rb_scan_args(argc, argv, "31", &digest, &rounds, &pass, &salt); + + md = EVP_get_digestbyname(STR2CSTR(digest)); + if (md == NULL) + ossl_raise(eCipherError, "invalid digest"); + + StringValue(pass); + + if (!NIL_P(salt)) { + StringValue(salt); + if (RSTRING(salt)->len != 8) + ossl_raise(eCipherError, "salt must be 8 bytes"); + } + + EVP_BytesToKey(EVP_CIPHER_CTX_cipher(ctx), md, NIL_P(salt) ? NULL : RSTRING(salt)->ptr, RSTRING(pass)->ptr, RSTRING(pass)->len, NUM2INT(rounds), key, iv); + if (EVP_CipherInit(ctx, NULL, key, iv, -1) != 1) + ossl_raise(eCipherError, ""); + + return self; +} + static VALUE ossl_cipher_update(VALUE self, VALUE data) { @@ -360,6 +390,8 @@ Init_ossl_cipher(void) rb_define_method(cCipher, "name", ossl_cipher_name, 0); + rb_define_method(cCipher, "pkcs5_v15_password", ossl_cipher_pkcs5_v15_password, -1); + rb_define_method(cCipher, "key=", ossl_cipher_set_key, 1); rb_define_method(cCipher, "key_len", ossl_cipher_key_length, 0); /* |