Search in sources :

Example 31 with ByteList

use of org.jruby.util.ByteList in project jruby-openssl by jruby.

the class Cipher method update.

@JRubyMethod
public IRubyObject update(final ThreadContext context, final IRubyObject arg) {
    final Ruby runtime = context.runtime;
    if (isDebug(runtime))
        dumpVars(runtime.getOut(), "update()");
    checkCipherNotNull(runtime);
    checkAuthTag(runtime);
    final ByteList data = arg.asString().getByteList();
    final int length = data.getRealSize();
    if (length == 0) {
        throw runtime.newArgumentError("data must not be empty");
    }
    if (!cipherInited)
        doInitCipher(runtime);
    final ByteList str;
    try {
        // if any
        updateAuthData(runtime);
        final byte[] in = data.getUnsafeBytes();
        final int offset = data.begin();
        final byte[] out = cipher.update(in, offset, length);
        if (out != null) {
            str = new ByteList(out, false);
            if (realIV != null) {
                if (encryptMode)
                    setLastIVIfNeeded(out);
                else
                    setLastIVIfNeeded(in, offset, length);
            }
            processedDataBytes += length;
        } else {
            str = new ByteList(ByteList.NULL_ARRAY);
        }
    } catch (Exception e) {
        debugStackTrace(runtime, e);
        throw newCipherError(runtime, e);
    }
    return RubyString.newString(runtime, str);
}
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 32 with ByteList

use of org.jruby.util.ByteList in project jruby-openssl by jruby.

the class Digest method update.

@JRubyMethod(name = "update", alias = "<<")
public IRubyObject update(final IRubyObject obj) {
    final ByteList bytes = obj.asString().getByteList();
    digest.update(bytes.getUnsafeBytes(), bytes.getBegin(), bytes.getRealSize());
    return this;
}
Also used : ByteList(org.jruby.util.ByteList) JRubyMethod(org.jruby.anno.JRubyMethod)

Example 33 with ByteList

use of org.jruby.util.ByteList in project jruby-openssl by jruby.

the class HMAC method digest.

@JRubyMethod(name = "digest", meta = true)
public static IRubyObject digest(IRubyObject self, IRubyObject digest, IRubyObject key, IRubyObject data) {
    final Ruby runtime = self.getRuntime();
    final String algName = getDigestAlgorithmName(digest);
    final byte[] keyBytes = key.asString().getBytes();
    final ByteList bytes = data.asString().getByteList();
    try {
        Mac mac = getMacInstance(algName);
        mac.init(new SimpleSecretKey(mac.getAlgorithm(), keyBytes));
        mac.update(bytes.getUnsafeBytes(), bytes.getBegin(), bytes.getRealSize());
        return runtime.newString(new ByteList(mac.doFinal(), false));
    } catch (NoSuchAlgorithmException e) {
        throw runtime.newNotImplementedError("Unsupported MAC algorithm (HMAC[-]" + algName + ")");
    } catch (GeneralSecurityException e) {
        if (isDebug(runtime))
            e.printStackTrace(runtime.getOut());
        throw runtime.newNotImplementedError(e.getMessage());
    }
}
Also used : ByteList(org.jruby.util.ByteList) GeneralSecurityException(java.security.GeneralSecurityException) RubyString(org.jruby.RubyString) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) Ruby(org.jruby.Ruby) Mac(javax.crypto.Mac) JRubyMethod(org.jruby.anno.JRubyMethod)

Example 34 with ByteList

use of org.jruby.util.ByteList in project jruby-openssl by jruby.

the class HMAC method initialize_copy.

@Override
@JRubyMethod(visibility = Visibility.PRIVATE)
public IRubyObject initialize_copy(final IRubyObject obj) {
    if (this == obj)
        return this;
    checkFrozen();
    final HMAC that = ((HMAC) obj);
    final String algName = that.mac.getAlgorithm();
    try {
        this.mac = SecurityHelper.getMac(algName);
        this.key = that.key;
        mac.init(SimpleSecretKey.copy(algName, key));
    } catch (NoSuchAlgorithmException e) {
        throw getRuntime().newNotImplementedError("Unsupported MAC algorithm (" + algName + ")");
    } catch (GeneralSecurityException e) {
        if (isDebug(getRuntime()))
            e.printStackTrace(getRuntime().getOut());
        throw getRuntime().newNotImplementedError(e.getMessage());
    }
    data = new ByteList(that.data);
    return this;
}
Also used : ByteList(org.jruby.util.ByteList) GeneralSecurityException(java.security.GeneralSecurityException) RubyString(org.jruby.RubyString) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) JRubyMethod(org.jruby.anno.JRubyMethod)

Example 35 with ByteList

use of org.jruby.util.ByteList in project jruby-openssl by jruby.

the class HMAC method hexdigest.

@JRubyMethod(name = "hexdigest", meta = true)
public static IRubyObject hexdigest(IRubyObject self, IRubyObject digest, IRubyObject key, IRubyObject data) {
    final Ruby runtime = self.getRuntime();
    final String algName = getDigestAlgorithmName(digest);
    final byte[] keyBytes = key.asString().getBytes();
    final ByteList bytes = data.asString().getByteList();
    try {
        final Mac mac = getMacInstance(algName);
        mac.init(new SimpleSecretKey(mac.getAlgorithm(), keyBytes));
        mac.update(bytes.getUnsafeBytes(), bytes.getBegin(), bytes.getRealSize());
        return runtime.newString(toHEX(mac.doFinal()));
    } catch (NoSuchAlgorithmException e) {
        throw runtime.newNotImplementedError("Unsupported MAC algorithm (HMAC[-]" + algName + ")");
    } catch (GeneralSecurityException e) {
        if (isDebug(runtime))
            e.printStackTrace(runtime.getOut());
        throw runtime.newNotImplementedError(e.getMessage());
    }
}
Also used : ByteList(org.jruby.util.ByteList) GeneralSecurityException(java.security.GeneralSecurityException) RubyString(org.jruby.RubyString) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) Ruby(org.jruby.Ruby) Mac(javax.crypto.Mac) JRubyMethod(org.jruby.anno.JRubyMethod)

Aggregations

ByteList (org.jruby.util.ByteList)43 Ruby (org.jruby.Ruby)21 JRubyMethod (org.jruby.anno.JRubyMethod)19 RubyString (org.jruby.RubyString)18 IRubyObject (org.jruby.runtime.builtin.IRubyObject)10 GeneralSecurityException (java.security.GeneralSecurityException)8 IOException (java.io.IOException)6 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)6 ByteBuffer (java.nio.ByteBuffer)5 NokogiriHelpers.rubyStringToString (nokogiri.internals.NokogiriHelpers.rubyStringToString)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 BigInteger (java.math.BigInteger)3 InvalidKeyException (java.security.InvalidKeyException)3 NoSuchPaddingException (javax.crypto.NoSuchPaddingException)3 ASN1ObjectIdentifier (org.bouncycastle.asn1.ASN1ObjectIdentifier)3 ASN1OctetString (org.bouncycastle.asn1.ASN1OctetString)3 ASN1Sequence (org.bouncycastle.asn1.ASN1Sequence)3 ASN1TaggedObject (org.bouncycastle.asn1.ASN1TaggedObject)3 InputSource (org.xml.sax.InputSource)3 StringReader (java.io.StringReader)2