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;
}
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());
}
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;
}
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;
}
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();
}
Aggregations