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