本文整理汇总了Java中org.bitcoinj.crypto.KeyCrypterScrypt.deriveKey方法的典型用法代码示例。如果您正苦于以下问题:Java KeyCrypterScrypt.deriveKey方法的具体用法?Java KeyCrypterScrypt.deriveKey怎么用?Java KeyCrypterScrypt.deriveKey使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.bitcoinj.crypto.KeyCrypterScrypt
的用法示例。
在下文中一共展示了KeyCrypterScrypt.deriveKey方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: IdealPasswordParameters
import org.bitcoinj.crypto.KeyCrypterScrypt; //导入方法依赖的package包/类
public IdealPasswordParameters(String password) {
final int targetTimeMsec = 2000;
int iterations = 16384;
KeyCrypterScrypt scrypt = new KeyCrypterScrypt(iterations);
long now = System.currentTimeMillis();
scrypt.deriveKey(password);
long time = System.currentTimeMillis() - now;
log.info("Initial iterations took {} msec", time);
// N can only be a power of two, so we keep shifting both iterations and doubling time taken
// until we are in sorta the right general area.
while (time < targetTimeMsec) {
iterations <<= 1;
time *= 2;
}
realIterations = iterations;
// Fudge it by +10% to ensure our progress meter is always a bit behind the real encryption. Plus
// without this it seems the real scrypting always takes a bit longer than we estimated for some reason.
realTargetTime = Duration.ofMillis((long) (time * 1.1));
}
示例2: deterministicUpgradeEncrypted
import org.bitcoinj.crypto.KeyCrypterScrypt; //导入方法依赖的package包/类
@Test
public void deterministicUpgradeEncrypted() throws Exception {
group = new KeyChainGroup(params);
final ECKey key = new ECKey();
group.importKeys(key);
final KeyCrypterScrypt crypter = new KeyCrypterScrypt();
final KeyParameter aesKey = crypter.deriveKey("abc");
assertTrue(group.isDeterministicUpgradeRequired());
group.encrypt(crypter, aesKey);
assertTrue(group.isDeterministicUpgradeRequired());
try {
group.upgradeToDeterministic(0, null);
fail();
} catch (DeterministicUpgradeRequiresPassword e) {
// Expected.
}
group.upgradeToDeterministic(0, aesKey);
assertFalse(group.isDeterministicUpgradeRequired());
final DeterministicSeed deterministicSeed = group.getActiveKeyChain().getSeed();
assertNotNull(deterministicSeed);
assertTrue(deterministicSeed.isEncrypted());
byte[] entropy = checkNotNull(group.getActiveKeyChain().toDecrypted(aesKey).getSeed()).getEntropyBytes();
// Check we used the right key: oldest non rotating.
byte[] truncatedBytes = Arrays.copyOfRange(key.getSecretBytes(), 0, 16);
assertArrayEquals(entropy, truncatedBytes);
}
示例3: doInBackground
import org.bitcoinj.crypto.KeyCrypterScrypt; //导入方法依赖的package包/类
protected Wallet doInBackground(Void... params) {
Intent intent = new Intent(CoinService.ACTION_CLEAR_CONNECTIONS, null,
walletApplication, CoinServiceImpl.class);
walletApplication.startService(intent);
ArrayList<String> seedWords = new ArrayList<String>();
for (String word : seed.trim().split(" ")) {
if (word.isEmpty()) continue;
seedWords.add(word);
}
try {
this.publishProgress("");
walletApplication.setEmptyWallet();
wallet = new Wallet(seedWords, seedPassword);
KeyParameter aesKey = null;
if (password != null && !password.isEmpty()) {
KeyCrypterScrypt crypter = new KeyCrypterScrypt();
aesKey = crypter.deriveKey(password);
wallet.encrypt(crypter, aesKey);
}
for (CoinType type : coinsToCreate) {
this.publishProgress(type.getName());
wallet.createAccount(type, false, aesKey);
}
walletApplication.setWallet(wallet);
walletApplication.saveWalletNow();
} catch (Exception e) {
log.error("Error creating a wallet", e);
errorMessage = e.getMessage();
}
return wallet;
}
示例4: encryptionWhilstEmpty
import org.bitcoinj.crypto.KeyCrypterScrypt; //导入方法依赖的package包/类
@Test
public void encryptionWhilstEmpty() throws Exception {
group = new KeyChainGroup(params);
group.setLookaheadSize(5);
KeyCrypterScrypt scrypt = new KeyCrypterScrypt(2);
final KeyParameter aesKey = scrypt.deriveKey("password");
group.encrypt(scrypt, aesKey);
assertTrue(group.freshKey(KeyChain.KeyPurpose.RECEIVE_FUNDS).isEncrypted());
final ECKey key = group.currentKey(KeyChain.KeyPurpose.RECEIVE_FUNDS);
group.decrypt(aesKey);
assertFalse(checkNotNull(group.findKeyFromPubKey(key.getPubKey())).isEncrypted());
}
示例5: serialization
import org.bitcoinj.crypto.KeyCrypterScrypt; //导入方法依赖的package包/类
@Test
public void serialization() throws Exception {
System.out.println("KeyChainGroupTest#serialisation group:" + group.getActiveKeyChain().getKeys(true).toString());
assertEquals(INITIAL_KEYS + 1 /* for the seed */, group.serializeToProtobuf().size());
group = KeyChainGroup.fromProtobufUnencrypted(params, group.serializeToProtobuf());
group.freshKey(KeyChain.KeyPurpose.RECEIVE_FUNDS);
DeterministicKey key1 = group.freshKey(KeyChain.KeyPurpose.RECEIVE_FUNDS);
DeterministicKey key2 = group.freshKey(KeyChain.KeyPurpose.CHANGE);
group.getBloomFilterElementCount();
List<Protos.Key> protoKeys1 = group.serializeToProtobuf();
assertEquals(INITIAL_KEYS + ((LOOKAHEAD_SIZE + 2) * 2) + 1 /* for the seed */ + 1, protoKeys1.size());
group.importKeys(new ECKey());
List<Protos.Key> protoKeys2 = group.serializeToProtobuf();
assertEquals(INITIAL_KEYS + ((LOOKAHEAD_SIZE + 2) * 2) + 1 /* for the seed */ + 2, protoKeys2.size());
group = KeyChainGroup.fromProtobufUnencrypted(params, protoKeys1);
assertEquals(INITIAL_KEYS + ((LOOKAHEAD_SIZE + 2) * 2) + 1 /* for the seed */ + 1, protoKeys1.size());
assertTrue(group.hasKey(key1));
assertTrue(group.hasKey(key2));
assertEquals(key2, group.currentKey(KeyChain.KeyPurpose.CHANGE));
assertEquals(key1, group.currentKey(KeyChain.KeyPurpose.RECEIVE_FUNDS));
group = KeyChainGroup.fromProtobufUnencrypted(params, protoKeys2);
assertEquals(INITIAL_KEYS + ((LOOKAHEAD_SIZE + 2) * 2) + 1 /* for the seed */ + 2, protoKeys2.size());
assertTrue(group.hasKey(key1));
assertTrue(group.hasKey(key2));
KeyCrypterScrypt scrypt = new KeyCrypterScrypt(2);
final KeyParameter aesKey = scrypt.deriveKey("password");
group.encrypt(scrypt, aesKey);
List<Protos.Key> protoKeys3 = group.serializeToProtobuf();
group = KeyChainGroup.fromProtobufEncrypted(params, protoKeys3, scrypt);
assertTrue(group.isEncrypted());
assertTrue(group.checkPassword("password"));
group.decrypt(aesKey);
// No need for extensive contents testing here, as that's done in the keychain class tests.
}
示例6: load
import org.bitcoinj.crypto.KeyCrypterScrypt; //导入方法依赖的package包/类
public void load(File file, String password) throws Exception {
ObjectMapper mapper = new ObjectMapper();
KeyStore keystore = mapper.readValue(new FileInputStream(file), KeyStore.class);
Protos.ScryptParameters.Builder scryptParametersBuilder
= Protos.ScryptParameters.newBuilder()
.setSalt(ByteString.copyFrom(base64Url().decode(keystore.getScrypt().getSalt())))
.setN(keystore.getScrypt().getN())
.setR(keystore.getScrypt().getR())
.setP(keystore.getScrypt().getP());
KeyCrypterScrypt crypterScrypt = new KeyCrypterScrypt(scryptParametersBuilder.build());
KeyParameter aesKey = crypterScrypt.deriveKey(password);
for (KeyEntry entry : keystore.getKeys()) {
byte[] key = base64Url().decode(entry.getPk());
byte[] iv = base64Url().decode(entry.getIv());
EncryptedData data = new EncryptedData(iv, key);
ECKey ecKey = ECKey.fromPrivate(crypterScrypt.decrypt(data, aesKey));
KeyContentValues kcv = new KeyContentValues();
kcv.putNickname(entry.getAlias());
kcv.putAddress(ecKey.toAddress(MainNetParams.get()).toString());
kcv.putPriv(ecKey.getPrivKeyBytes());
kcv.putPub(ecKey.getPubKey());
KeySelection ks = new KeySelection();
ks.nickname(entry.getAlias());
if(kcv.update(mContext.getContentResolver(), ks) != 1) {
kcv.insert(mContext.getContentResolver());
};
}
}
示例7: KeyDerivationTasks
import org.bitcoinj.crypto.KeyCrypterScrypt; //导入方法依赖的package包/类
public KeyDerivationTasks(KeyCrypterScrypt scrypt, String password, @Nullable Duration targetTime) {
keyDerivationTask = new Task<KeyParameter>() {
@Override
protected KeyParameter call() throws Exception {
long start = System.currentTimeMillis();
try {
log.info("Started key derivation");
KeyParameter result = scrypt.deriveKey(password);
timeTakenMsec = (int) (System.currentTimeMillis() - start);
log.info("Key derivation done in {}ms", timeTakenMsec);
return result;
} catch (Throwable e) {
log.error("Exception during key derivation", e);
throw e;
}
}
};
// And the fake progress meter ... if the vals were calculated correctly progress bar should reach 100%
// a brief moment after the keys were derived successfully.
progressTask = new Task<Void>() {
private KeyParameter aesKey;
@Override
protected Void call() throws Exception {
if (targetTime != null) {
long startTime = System.currentTimeMillis();
long curTime;
long targetTimeMillis = targetTime.toMillis();
while ((curTime = System.currentTimeMillis()) < startTime + targetTimeMillis) {
double progress = (curTime - startTime) / (double) targetTimeMillis;
updateProgress(progress, 1.0);
// 60fps would require 16msec sleep here.
Uninterruptibles.sleepUninterruptibly(20, TimeUnit.MILLISECONDS);
}
// Wait for the encryption thread before switching back to main UI.
updateProgress(1.0, 1.0);
} else {
updateProgress(-1, -1);
}
aesKey = keyDerivationTask.get();
return null;
}
@Override
protected void succeeded() {
checkGuiThread();
onFinish(aesKey, timeTakenMsec);
}
};
progress = progressTask.progressProperty();
}
示例8: KeyDerivationTasks
import org.bitcoinj.crypto.KeyCrypterScrypt; //导入方法依赖的package包/类
public KeyDerivationTasks (KeyCrypterScrypt scrypt, String password, @Nullable Duration targetTime) {
keyDerivationTask = new Task<KeyParameter>() {
@Override
protected KeyParameter call () throws Exception {
long start = System.currentTimeMillis();
try {
log.info("Started key derivation");
KeyParameter result = scrypt.deriveKey(password);
timeTakenMsec = (int) (System.currentTimeMillis() - start);
log.info("Key derivation done in {}ms", timeTakenMsec);
return result;
} catch (Throwable e) {
log.error("Exception during key derivation", e);
throw e;
}
}
};
// And the fake progress meter ... if the vals were calculated correctly progress bar should reach 100%
// a brief moment after the keys were derived successfully.
progressTask = new Task<Void>() {
private KeyParameter aesKey;
@Override
protected Void call () throws Exception {
if (targetTime != null) {
long startTime = System.currentTimeMillis();
long curTime;
long targetTimeMillis = targetTime.toMillis();
while ((curTime = System.currentTimeMillis()) < startTime + targetTimeMillis) {
double progress = (curTime - startTime) / (double) targetTimeMillis;
updateProgress(progress, 1.0);
// 60fps would require 16msec sleep here.
Uninterruptibles.sleepUninterruptibly(20, TimeUnit.MILLISECONDS);
}
// Wait for the encryption thread before switching back to main UI.
updateProgress(1.0, 1.0);
} else {
updateProgress(-1, -1);
}
aesKey = keyDerivationTask.get();
return null;
}
@Override
protected void succeeded () {
checkGuiThread();
onFinish(aesKey, timeTakenMsec);
}
};
progress = progressTask.progressProperty();
}
示例9: KeyDerivationTasks
import org.bitcoinj.crypto.KeyCrypterScrypt; //导入方法依赖的package包/类
public KeyDerivationTasks(KeyCrypterScrypt scrypt, String password, Duration targetTime) {
keyDerivationTask = new Task<KeyParameter>() {
@Override
protected KeyParameter call() throws Exception {
try {
return scrypt.deriveKey(password);
} catch (Throwable e) {
e.printStackTrace();
throw e;
} finally {
log.info("Key derivation done");
}
}
};
// And the fake progress meter ...
progressTask = new Task<Void>() {
private KeyParameter aesKey;
@Override
protected Void call() throws Exception {
long startTime = System.currentTimeMillis();
long curTime;
long targetTimeMillis = targetTime.toMillis();
while ((curTime = System.currentTimeMillis()) < startTime + targetTimeMillis) {
double progress = (curTime - startTime) / (double) targetTimeMillis;
updateProgress(progress, 1.0);
// 60fps would require 16msec sleep here.
Uninterruptibles.sleepUninterruptibly(20, TimeUnit.MILLISECONDS);
}
// Wait for the encryption thread before switching back to main UI.
updateProgress(1.0, 1.0);
aesKey = keyDerivationTask.get();
return null;
}
@Override
protected void succeeded() {
checkGuiThread();
onFinish(aesKey);
}
};
progress = progressTask.progressProperty();
}
示例10: store
import org.bitcoinj.crypto.KeyCrypterScrypt; //导入方法依赖的package包/类
public void store(OutputStream os, String password)
throws Exception {
final Cursor c = mContext.getContentResolver()
.query(KeyColumns.CONTENT_URI, null, null, null, null);
KeyCursor kc = new KeyCursor(c);
kc.moveToFirst();
KeyCrypterScrypt crypterScrypt = new KeyCrypterScrypt(512);
KeyParameter aesKey = crypterScrypt.deriveKey(password);
Protos.ScryptParameters params = crypterScrypt.getScryptParameters();
ScryptHeader scryptHeader = new ScryptHeader();
scryptHeader.setN(params.getN());
scryptHeader.setR(params.getR());
scryptHeader.setP(params.getP());
scryptHeader.setSalt(BaseEncoding.base64Url().encode(params.getSalt().toByteArray()));
KeyStore keyStore = new KeyStore();
keyStore.setScrypt(scryptHeader);
for (int i = 0; i < kc.getCount(); i++) {
kc.moveToPosition(i);
EncryptedData data = crypterScrypt.encrypt(kc.getPriv(), aesKey);
String ecKey64 = BaseEncoding.base64Url().encode(data.encryptedBytes);
KeyEntry entry = new KeyEntry();
entry.setAlias(kc.getNickname());
entry.setKty("EC");
entry.setCrv("P-256");
entry.setPk(ecKey64);
entry.setIv(BaseEncoding.base64Url().encode(data.initialisationVector));
keyStore.addKey(entry);
}
ObjectMapper mapper = new ObjectMapper();
mapper.writeValue(os, keyStore);
os.close();
}