use of org.spongycastle.crypto.params.KeyParameter in project bitsquare by bitsquare.
the class WalletService method getSendRequestForMultipleAddresses.
private Wallet.SendRequest getSendRequestForMultipleAddresses(Set<String> fromAddresses, String toAddress, Coin amount, @Nullable String changeAddress, @Nullable KeyParameter aesKey) throws AddressFormatException, AddressEntryException, InsufficientMoneyException {
Transaction tx = new Transaction(params);
Preconditions.checkArgument(Restrictions.isAboveDust(amount), "The amount is too low (dust limit).");
tx.addOutput(amount, new Address(params, toAddress));
Wallet.SendRequest sendRequest = Wallet.SendRequest.forTx(tx);
sendRequest.aesKey = aesKey;
sendRequest.shuffleOutputs = false;
Set<AddressEntry> addressEntries = fromAddresses.stream().map(address -> {
Optional<AddressEntry> addressEntryOptional = findAddressEntry(address, AddressEntry.Context.AVAILABLE);
if (!addressEntryOptional.isPresent())
addressEntryOptional = findAddressEntry(address, AddressEntry.Context.OFFER_FUNDING);
if (!addressEntryOptional.isPresent())
addressEntryOptional = findAddressEntry(address, AddressEntry.Context.TRADE_PAYOUT);
if (!addressEntryOptional.isPresent())
addressEntryOptional = findAddressEntry(address, AddressEntry.Context.ARBITRATOR);
return addressEntryOptional;
}).filter(Optional::isPresent).map(Optional::get).collect(Collectors.toSet());
if (addressEntries.isEmpty())
throw new AddressEntryException("No Addresses for withdraw found in our wallet");
sendRequest.coinSelector = new MultiAddressesCoinSelector(params, addressEntries);
Optional<AddressEntry> addressEntryOptional = Optional.empty();
AddressEntry changeAddressAddressEntry = null;
if (changeAddress != null)
addressEntryOptional = findAddressEntry(changeAddress, AddressEntry.Context.AVAILABLE);
if (addressEntryOptional.isPresent()) {
changeAddressAddressEntry = addressEntryOptional.get();
} else {
ArrayList<AddressEntry> list = new ArrayList<>(addressEntries);
if (!list.isEmpty())
changeAddressAddressEntry = list.get(0);
}
checkNotNull(changeAddressAddressEntry, "change address must not be null");
sendRequest.changeAddress = changeAddressAddressEntry.getAddress();
sendRequest.feePerKb = FeePolicy.getNonTradeFeePerKb();
return sendRequest;
}
use of org.spongycastle.crypto.params.KeyParameter in project conceal by facebook.
the class BouncyCastleHelper method bouncyCastleEncrypt.
public static Result bouncyCastleEncrypt(byte[] data, byte[] key, byte[] iv, byte[] aadData) throws UnsupportedEncodingException, InvalidCipherTextException {
GCMBlockCipher gcm = new GCMBlockCipher(new AESEngine());
byte[] gcmOut = new byte[CryptoTestUtils.NUM_DATA_BYTES + CryptoConfig.KEY_128.tagLength];
KeyParameter keyParameter = new KeyParameter(key);
// Add aad data.
AEADParameters params = new AEADParameters(keyParameter, CryptoConfig.KEY_128.tagLength * 8, iv, aadData);
// Init encryption.
gcm.init(true, params);
int written = gcm.processBytes(data, 0, data.length, gcmOut, 0);
written += gcm.doFinal(gcmOut, written);
byte[] bouncyCastleOut = Arrays.copyOfRange(gcmOut, 0, written);
byte[] cipherText = Arrays.copyOfRange(bouncyCastleOut, 0, CryptoTestUtils.NUM_DATA_BYTES);
byte[] tag = Arrays.copyOfRange(bouncyCastleOut, CryptoTestUtils.NUM_DATA_BYTES, bouncyCastleOut.length);
return new Result(cipherText, tag);
}
use of org.spongycastle.crypto.params.KeyParameter in project bitsquare by bitsquare.
the class ScryptUtil method deriveKeyWithScrypt.
public static void deriveKeyWithScrypt(KeyCrypterScrypt keyCrypterScrypt, String password, DeriveKeyResultHandler resultHandler) {
Utilities.getThreadPoolExecutor("ScryptUtil:deriveKeyWithScrypt-%d", 1, 2, 5L).submit(() -> {
try {
log.debug("Doing key derivation");
long start = System.currentTimeMillis();
KeyParameter aesKey = keyCrypterScrypt.deriveKey(password);
long duration = System.currentTimeMillis() - start;
log.debug("Key derivation took {} msec", duration);
UserThread.execute(() -> {
try {
resultHandler.handleResult(aesKey);
} catch (Throwable t) {
t.printStackTrace();
log.error("Executing task failed. " + t.getMessage());
}
});
} catch (Throwable t) {
t.printStackTrace();
log.error("Executing task failed. " + t.getMessage());
}
});
}
Aggregations