BTC的消息签名机制,是使用私钥对一段信息签名,然后使用公钥对此签名做校验,次机制可以作为验证消息真伪的手段,这里使用bitcoinj库实现的
implementation ‘org.bitcoinj:bitcoinj-core:0.14.7’
对消息进行签名
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
public static String signMsg(@NonNull String msg, @NonNull String privateKey) { NetworkParameters networkParameters = null; if (!BTC_TEST_NET) networkParameters = MainNetParams.get(); else networkParameters = TestNet3Params.get(); DumpedPrivateKey priKey = DumpedPrivateKey.fromBase58(networkParameters, privateKey); ECKey ecKey = priKey.getKey(); return ecKey.signMessage(msg); }
|
验证签名消息
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
|
public static boolean verifyMessage(@NonNull String msg, @NonNull String signatureMsg, @NonNull String pubkey) { boolean result = false; ECKey ecKey = ECKey.fromPublicOnly(Utils.HEX.decode(pubkey)); try { ecKey.verifyMessage(msg, signatureMsg); result = true; } catch (SignatureException e) { result = false; e.printStackTrace(); } finally { return result; }
}
|