@Data
@NoArgsConstructor
@AllArgsConstructor
public class Wallet implements Serializable {
private static final long serialVersionUID = -7848786859278946662L;
private String address;
private String publicKey;
private String privateKey;
public static Wallet generateNewWallet() {
KeyPairGenerator keyPairGen;
try {
keyPairGen = KeyPairGenerator.getInstance("RSA");
} catch (Exception e) {
return null;
}
keyPairGen.initialize(512);
KeyPair keyPair = keyPairGen.generateKeyPair();
RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();
RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();
String publicKeyStr = Base64.encodeBase64String(publicKey.getEncoded());
String privateKeyStr = Base64.encodeBase64String(privateKey.getEncoded());
String address = Hashing.md5().newHasher().putString(publicKeyStr, Charsets.UTF_8).hash().toString();
return new Wallet(address, publicKeyStr, privateKeyStr);
}
}
|