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