use of io.nuls.core.tools.crypto.ECKey in project nuls by nuls-io.
the class AccountServiceImpl method signMessage.
/**
* @auther EdwardChan
*
* @since Mar. 20th 2019
*
* 对消息进行签名
*
* @param address 对消息进行签名的地址
*
* @param password 账户的密码(如果已经设置密码则密码输入)
*
* @param message 需要签名的字符串
*
* @return 签名结果字符串
*/
@Override
public Result<String> signMessage(String address, String password, String message) throws NulsException {
String signatureBase64;
// 计算签名
// 查询出账户私钥
Account account = getAccountByAddress(address);
if (null == account) {
return Result.getFailed(AccountErrorCode.ACCOUNT_NOT_EXIST);
}
if (account.isEncrypted() && account.isLocked()) {
if (!account.unlock(password)) {
return Result.getFailed(AccountErrorCode.PASSWORD_IS_WRONG);
}
}
// 将消息加盐
message = AccountConstant.SIGN_MESSAGE_SALT + message;
ECKey key = ECKey.fromPrivate(new BigInteger(account.getPriKey()));
signatureBase64 = key.signMessage(message, null);
return Result.getSuccess().setData(signatureBase64);
}
use of io.nuls.core.tools.crypto.ECKey in project nuls by nuls-io.
the class AddressTest method test.
@Test
public void test() {
short chainId = 8964;
NulsContext.getInstance().defaultChainId = chainId;
ECKey ecKey = ECKey.fromPrivate(new BigInteger(1, Hex.decode("60a958883bee99abc16b59a191ae25f8854c5edb2daea7ee1e19425b728edf6a")));
System.out.println(ecKey.getPublicKeyAsHex(true));
// while (true) {
// ECKey ecKey = new ECKey();
// String address = getAddress(chainId, ecKey.getPubKey());
// System.out.println(address );//+ ":::::::" + ecKey.getPrivateKeyAsHex());
// }
System.out.println(getAddress(chainId, Hex.decode("0369926e4c82ab5499a14ef115e1d2d0d4dfb1c44b4de46434e79fc3da928d29ab")));
}
use of io.nuls.core.tools.crypto.ECKey in project nuls by nuls-io.
the class Account method encrypt.
/**
* 根据密码加密账户(给账户设置密码)
* Password-encrypted account (set password for account)
*/
public void encrypt(String password, boolean isForce) throws NulsException {
if (this.isEncrypted()) {
if (isForce) {
if (isLocked()) {
throw new NulsException(AccountErrorCode.ACCOUNT_IS_ALREADY_ENCRYPTED_AND_LOCKED);
}
} else {
throw new NulsException(AccountErrorCode.ACCOUNT_IS_ALREADY_ENCRYPTED);
}
}
ECKey eckey = this.getEcKey();
byte[] privKeyBytes = eckey.getPrivKeyBytes();
EncryptedData encryptedPrivateKey = AESEncrypt.encrypt(privKeyBytes, EncryptedData.DEFAULT_IV, new KeyParameter(Sha256Hash.hash(password.getBytes())));
eckey.setEncryptedPrivateKey(encryptedPrivateKey);
ECKey result = ECKey.fromEncrypted(encryptedPrivateKey, getPubKey());
this.setPriKey(new byte[0]);
this.setEcKey(result);
this.setEncryptedPriKey(encryptedPrivateKey.getEncryptedBytes());
}
Aggregations