Last active
August 11, 2019 18:24
-
-
Save knez/ad087ad92ca1c3c36e9f9f93c0fd22bf to your computer and use it in GitHub Desktop.
Bitcoin HEX private key to WIF
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.util.Arrays; | |
| import java.security.MessageDigest; | |
| /** | |
| * Converts a 64 characters long hex private key | |
| * into a base58 WIF private key (starts with a '5') | |
| */ | |
| public class HexToWIF | |
| { | |
| public static byte[] getExtendedKey(String s) | |
| { | |
| int len = (s.length() / 2) + 1; | |
| byte[] data = new byte[len]; | |
| data[0] = (byte) 0x80; | |
| for (int i = 0; i < s.length(); i += 2) | |
| { | |
| data[(i + 2) / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4) | |
| + Character.digit(s.charAt(i+1), 16)); | |
| } | |
| return data; | |
| } | |
| public static void main(String[] args) | |
| { | |
| if (args.length != 1) | |
| { | |
| System.out.println("Wrong number of arguments"); | |
| return; | |
| } | |
| byte[] extendedKey = getExtendedKey(args[0]); | |
| try { | |
| var sha = MessageDigest.getInstance("SHA-256"); | |
| byte[] hash = sha.digest(extendedKey); | |
| hash = sha.digest(hash); | |
| extendedKey = Arrays.copyOf(extendedKey, 37); | |
| System.arraycopy(hash, 0, extendedKey, 33, 4); | |
| String privateKey = Base58.encode(extendedKey); | |
| System.out.println(privateKey); | |
| } | |
| catch (Exception e) { | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment