Search in sources :

Example 11 with ByteList

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

the class Digest method hexString.

private static RubyString hexString(final RubyString str) {
    final byte[] plain = str.getBytes();
    final int len = plain.length;
    // modify str in-place !
    final ByteList bytes = str.getByteList();
    bytes.length(len * 2);
    bytes.invalidate();
    final byte[] unsafeBytes = bytes.getUnsafeBytes();
    int index = bytes.getBegin();
    for (int i = 0; i < len; i++) {
        final int b = plain[i] & 0xFF;
        unsafeBytes[index++] = HEX[b >> 4];
        unsafeBytes[index++] = HEX[b & 0xF];
    }
    return str;
}
Also used : ByteList(org.jruby.util.ByteList)

Example 12 with ByteList

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

the class HMAC method toHEX.

private static ByteList toHEX(final byte[] data) {
    final ByteList out = new ByteList(data.length * 2);
    for (int i = 0; i < data.length; i++) {
        final byte b = data[i];
        out.append(HEX[(b >> 4) & 0xF]);
        out.append(HEX[b & 0xF]);
    }
    return out;
}
Also used : ByteList(org.jruby.util.ByteList)

Example 13 with ByteList

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

the class PKey method sign.

@JRubyMethod(name = "sign")
public IRubyObject sign(IRubyObject digest, IRubyObject data) {
    final Ruby runtime = getRuntime();
    if (!isPrivateKey())
        throw runtime.newArgumentError("Private key is needed.");
    String digAlg = (digest instanceof Digest) ? ((Digest) digest).getShortAlgorithm() : digest.asJavaString();
    try {
        ByteList sign = sign(digAlg + "WITH" + getAlgorithm(), getPrivateKey(), data.convertToString().getByteList());
        return RubyString.newString(runtime, sign);
    } catch (GeneralSecurityException ex) {
        throw newPKeyError(runtime, ex.getMessage());
    }
}
Also used : ByteList(org.jruby.util.ByteList) RubyString(org.jruby.RubyString) Ruby(org.jruby.Ruby) JRubyMethod(org.jruby.anno.JRubyMethod)

Example 14 with ByteList

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

the class PKey method verify.

@JRubyMethod(name = "verify")
public IRubyObject verify(IRubyObject digest, IRubyObject sign, IRubyObject data) {
    final Ruby runtime = getRuntime();
    ByteList sigBytes = convertToString(runtime, sign, "OpenSSL::PKey::PKeyError", "invalid signature").getByteList();
    ByteList dataBytes = convertToString(runtime, data, "OpenSSL::PKey::PKeyError", "invalid data").getByteList();
    String digAlg = (digest instanceof Digest) ? ((Digest) digest).getShortAlgorithm() : digest.asJavaString();
    final String algorithm = digAlg + "WITH" + getAlgorithm();
    try {
        return runtime.newBoolean(verify(algorithm, getPublicKey(), dataBytes, sigBytes));
    } catch (NoSuchAlgorithmException e) {
        throw newPKeyError(runtime, "unsupported algorithm: " + algorithm);
    } catch (SignatureException e) {
        throw newPKeyError(runtime, "invalid signature");
    } catch (InvalidKeyException e) {
        throw newPKeyError(runtime, "invalid key");
    }
}
Also used : ByteList(org.jruby.util.ByteList) RubyString(org.jruby.RubyString) Ruby(org.jruby.Ruby) JRubyMethod(org.jruby.anno.JRubyMethod)

Example 15 with ByteList

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

the class PKeyDH method compute_key.

@JRubyMethod(name = "compute_key")
public synchronized IRubyObject compute_key(IRubyObject other_pub_key) {
    BigInteger x, y, p;
    if ((y = BN.asBigInteger(other_pub_key)) == null) {
        throw getRuntime().newArgumentError("invalid public key");
    }
    if ((x = this.dh_x) == null || (p = this.dh_p) == null) {
        throw newDHError(getRuntime(), "incomplete DH");
    }
    int plen;
    if ((plen = p.bitLength()) == 0 || plen > OPENSSL_DH_MAX_MODULUS_BITS) {
        throw newDHError(getRuntime(), "can't compute key");
    }
    return getRuntime().newString(new ByteList(computeKey(y, x, p), false));
}
Also used : ByteList(org.jruby.util.ByteList) BigInteger(java.math.BigInteger) 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