Search in sources :

Example 41 with JRubyMethod

use of org.jruby.anno.JRubyMethod in project jruby-openssl by jruby.

the class Cipher method set_iv.

@JRubyMethod(name = "iv=", required = 1)
public IRubyObject set_iv(final ThreadContext context, final IRubyObject iv) {
    final ByteList ivBytes;
    try {
        ivBytes = iv.asString().getByteList();
    } catch (Exception e) {
        final Ruby runtime = context.runtime;
        debugStackTrace(runtime, e);
        throw newCipherError(runtime, e);
    }
    if (ivBytes.getRealSize() < ivLength) {
        throw newCipherError(context.runtime, "iv length too short");
    }
    // EVP_CipherInit_ex uses leading IV length of given sequence.
    final byte[] i = new byte[ivLength];
    System.arraycopy(ivBytes.unsafeBytes(), ivBytes.getBegin(), i, 0, ivLength);
    this.realIV = i;
    this.orgIV = this.realIV;
    if (!isStreamCipher())
        cipherInited = false;
    return iv;
}
Also used : ByteList(org.jruby.util.ByteList) Ruby(org.jruby.Ruby) RaiseException(org.jruby.exceptions.RaiseException) GeneralSecurityException(java.security.GeneralSecurityException) NoSuchPaddingException(javax.crypto.NoSuchPaddingException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(java.security.InvalidKeyException) JRubyMethod(org.jruby.anno.JRubyMethod)

Example 42 with JRubyMethod

use of org.jruby.anno.JRubyMethod in project jruby-openssl by jruby.

the class Cipher method block_size.

@JRubyMethod
public IRubyObject block_size(final ThreadContext context) {
    final Ruby runtime = context.runtime;
    checkCipherNotNull(runtime);
    if (isStreamCipher()) {
        // OpenSSL returns 1 for RC4.
        return runtime.newFixnum(1);
    }
    return runtime.newFixnum(cipher.getBlockSize());
}
Also used : Ruby(org.jruby.Ruby) JRubyMethod(org.jruby.anno.JRubyMethod)

Example 43 with JRubyMethod

use of org.jruby.anno.JRubyMethod in project jruby-openssl by jruby.

the class Cipher method set_auth_data.

@JRubyMethod(name = "auth_data=")
public IRubyObject set_auth_data(final ThreadContext context, final IRubyObject data) {
    if (!isAuthDataMode()) {
        throw newCipherError(context.runtime, "authentication data not supported by this cipher");
    }
    final RubyString auth_data = data.asString();
    this.auth_data = StringHelper.setByteListShared(auth_data);
    return auth_data;
}
Also used : RubyString(org.jruby.RubyString) JRubyMethod(org.jruby.anno.JRubyMethod)

Example 44 with JRubyMethod

use of org.jruby.anno.JRubyMethod in project jruby-openssl by jruby.

the class Cipher method random_key.

@JRubyMethod
public IRubyObject random_key(final ThreadContext context) {
    // str = OpenSSL::Random.random_bytes(self.key_len)
    // self.key = str
    // return str
    RubyString str = Random.random_bytes(context, this.keyLength);
    this.set_key(context, str);
    return str;
}
Also used : RubyString(org.jruby.RubyString) JRubyMethod(org.jruby.anno.JRubyMethod)

Example 45 with JRubyMethod

use of org.jruby.anno.JRubyMethod in project jruby-openssl by jruby.

the class Cipher method pkcs5_keyivgen.

@JRubyMethod(required = 1, optional = 3)
public IRubyObject pkcs5_keyivgen(final ThreadContext context, final IRubyObject[] args) {
    final Ruby runtime = context.runtime;
    Arity.checkArgumentCount(runtime, args, 1, 4);
    byte[] pass = args[0].asString().getBytes();
    byte[] salt = null;
    int iter = 2048;
    IRubyObject vdigest = runtime.getNil();
    if (args.length > 1) {
        if (!args[1].isNil()) {
            salt = args[1].asString().getBytes();
        }
        if (args.length > 2) {
            if (!args[2].isNil()) {
                iter = RubyNumeric.fix2int(args[2]);
            }
            if (args.length > 3) {
                vdigest = args[3];
            }
        }
    }
    if (salt != null && salt.length != 8) {
        throw newCipherError(runtime, "salt must be an 8-octet string");
    }
    final String algorithm;
    if (vdigest.isNil())
        algorithm = "MD5";
    else {
        algorithm = (vdigest instanceof Digest) ? ((Digest) vdigest).getAlgorithm() : vdigest.asJavaString();
    }
    final MessageDigest digest = Digest.getDigest(runtime, algorithm);
    KeyAndIv result = evpBytesToKey(keyLength, ivLength, digest, salt, pass, iter);
    this.key = result.key;
    this.realIV = result.iv;
    this.orgIV = this.realIV;
    doInitCipher(runtime);
    return runtime.getNil();
}
Also used : MessageDigest(java.security.MessageDigest) RubyString(org.jruby.RubyString) IRubyObject(org.jruby.runtime.builtin.IRubyObject) MessageDigest(java.security.MessageDigest) Ruby(org.jruby.Ruby) JRubyMethod(org.jruby.anno.JRubyMethod)

Aggregations

JRubyMethod (org.jruby.anno.JRubyMethod)304 Ruby (org.jruby.Ruby)157 RubyString (org.jruby.RubyString)83 IRubyObject (org.jruby.runtime.builtin.IRubyObject)77 IOException (java.io.IOException)49 RubyArray (org.jruby.RubyArray)34 Node (org.w3c.dom.Node)30 RaiseException (org.jruby.exceptions.RaiseException)27 BigInteger (java.math.BigInteger)24 GeneralSecurityException (java.security.GeneralSecurityException)24 NokogiriHelpers.rubyStringToString (nokogiri.internals.NokogiriHelpers.rubyStringToString)24 Document (org.w3c.dom.Document)20 ByteList (org.jruby.util.ByteList)19 RubyClass (org.jruby.RubyClass)16 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)14 RubyFloat (org.jruby.RubyFloat)13 JRubyClass (org.jruby.anno.JRubyClass)13 NokogiriHelpers.clearCachedNode (nokogiri.internals.NokogiriHelpers.clearCachedNode)12 RubyFixnum (org.jruby.RubyFixnum)12 Element (org.w3c.dom.Element)12