예제 #1
0
        /// <summary>
        /// if input         5Kek19qnxAqFsLXKToVMWcbpQryxzwaqtHLnQ9WwrZR8yC8aBck
        /// then hex result  f1b868e74dd6dd9d13a6bce594e62baf71fa367fc7747bdf019380adde153253
        /// </summary>
        /// <param name="wif"></param>
        /// <returns></returns>
        private static byte[] Wif2PrivateKey(string wif)
        {
            byte[] bytes        = Base58.Base58ToByteArray(wif);
            Byte[] privKeyAnd80 = new byte[bytes.Length - 4];
            Buffer.BlockCopy(bytes, 0, privKeyAnd80, 0, privKeyAnd80.Length);

            byte[] hash = new byte[4];
            using (var sha256 = new SHA256Managed())
                Buffer.BlockCopy(
                    sha256.ComputeHash(sha256.ComputeHash(privKeyAnd80)), 0,
                    hash, 0, 4);

            byte[] checkSum = new byte[4];
            Buffer.BlockCopy(bytes, privKeyAnd80.Length, checkSum, 0, 4);

            if (checkSum.SequenceEqual(hash) && (privKeyAnd80[0] == 0x80))
            {
                byte[] result = new byte[privKeyAnd80.Length - 1];
                Buffer.BlockCopy(privKeyAnd80, 1, result, 0, result.Length);
                return(result);
            }
            return(null);
        }