Ejemplo n.º 1
0
        private static string GenerateWif(string passphrase, Network network, LotSequence lotsequence, byte[] ownersalt)
        {
            bool hasLotSequence = lotsequence != null;

            //ownersalt is 8 random bytes
            ownersalt = ownersalt ?? RandomUtils.GetBytes(8);
            var ownerEntropy = ownersalt;

            if (hasLotSequence)
            {
                ownersalt    = ownersalt.Take(4).ToArray();
                ownerEntropy = ownersalt.Concat(lotsequence.ToBytes()).ToArray();
            }


            var prefactor  = SCrypt.CoinComputeDerivedKey(Encoding.UTF8.GetBytes(passphrase), ownersalt, 32);
            var passfactor = prefactor;

            if (hasLotSequence)
            {
                passfactor = Hashes.Hash256(prefactor.Concat(ownerEntropy).ToArray()).ToBytes();
            }

            var passpoint = new CCKey(passfactor, fCompressedIn: true).PubKey.ToBytes();

            var bytes =
                network.GetVersionBytes(Network.BASE58_PASSPHRASE_CODE, true)
                .Concat(new[] { hasLotSequence ? (byte)0x51 : (byte)0x53 })
                .Concat(ownerEntropy)
                .Concat(passpoint)
                .ToArray();

            return(Encoders.Base58Check.EncodeData(bytes));
        }
Ejemplo n.º 2
0
        private static string GenerateWif(CCKey key, string password, Network network)
        {
            var vch = key.ToBytes();
            //Compute the Coin address (ASCII),
            var addressBytes = Encoders.ASCII.DecodeData(key.PubKey.GetAddress(network).ToString());
            // and take the first four bytes of SHA256(SHA256()) of it. Let's call this "addresshash".
            var addresshash = Hashes.Hash256(addressBytes).ToBytes().SafeSubarray(0, 4);

            var derived = SCrypt.CoinComputeDerivedKey(Encoding.UTF8.GetBytes(password), addresshash);

            var encrypted = EncryptKey(vch, derived);



            var  version  = network.GetVersionBytes(Network.BASE58_ENCRYPTED_SECRET_KEY_NO_EC, true);
            byte flagByte = 0;

            flagByte |= 0x0C0;
            flagByte |= (key.IsCompressed ? (byte)0x20 : (byte)0x00);

            var bytes = version
                        .Concat(new[] { flagByte })
                        .Concat(addresshash)
                        .Concat(encrypted).ToArray();

            return(Encoders.Base58Check.EncodeData(bytes));
        }
Ejemplo n.º 3
0
 public static byte[] CalculatePassFactor(string password, LotSequence lotSequence, byte[] ownerEntropy)
 {
     byte[] passfactor;
     if (lotSequence == null)
     {
         passfactor = SCrypt.CoinComputeDerivedKey(Encoding.UTF8.GetBytes(password), ownerEntropy, 32);
     }
     else
     {
         var ownersalt = ownerEntropy.SafeSubarray(0, 4);
         var prefactor = SCrypt.CoinComputeDerivedKey(Encoding.UTF8.GetBytes(password), ownersalt, 32);
         passfactor = Hashes.Hash256(prefactor.Concat(ownerEntropy).ToArray()).ToBytes();
     }
     return(passfactor);
 }
Ejemplo n.º 4
0
        public override CCKey GetKey(string password)
        {
            var derived      = SCrypt.CoinComputeDerivedKey(password, AddressHash);
            var lCoinprivkey = DecryptKey(Encrypted, derived);

            var key = new CCKey(lCoinprivkey, fCompressedIn: IsCompressed);

            var addressBytes = Encoders.ASCII.DecodeData(key.PubKey.GetAddress(Network).ToString());
            var salt         = Hashes.Hash256(addressBytes).ToBytes().SafeSubarray(0, 4);

            if (!Utils.ArrayEqual(salt, AddressHash))
            {
                throw new SecurityException("Invalid password (or invalid Network)");
            }
            return(key);
        }