コード例 #1
0
        public PrivateKey(string label, bool eighteenByteRipe = false)
        {
            var startTime = DateTime.Now;

            Label = label;

            RNGCryptoServiceProvider rnd = new RNGCryptoServiceProvider();

            byte[] potentialPrivSigningKey = new byte[32];
            rnd.GetBytes(potentialPrivSigningKey);
            SigningKey = ECDSA.PointMult(potentialPrivSigningKey);

            int numberOfAddressesWeHadToMakeBeforeWeFoundOneWithTheCorrectRipePrefix = 0;

            byte[] ripemd160;
            byte[] potentialPrivEncryptionKey = new byte[32];

            while (true)
            {
                numberOfAddressesWeHadToMakeBeforeWeFoundOneWithTheCorrectRipePrefix += 1;
                rnd.GetBytes(potentialPrivEncryptionKey);

                EncryptionKey = ECDSA.PointMult(potentialPrivEncryptionKey);

                byte[] buff = SigningKey.Concatenate(EncryptionKey);
                byte[] sha  = new SHA512Managed().ComputeHash(buff);
                ripemd160 = RIPEMD160.Create().ComputeHash(sha);

                if (eighteenByteRipe)
                {
                    if ((ripemd160[0] == 0) && (ripemd160[1] == 0))
                    {
                        break;
                    }
                }
                else
                {
                    if (ripemd160[0] == 0)
                    {
                        break;
                    }
                }
            }

            // NonceTrialsPerByte используется значение по умолчанию
            // payloadLengthExtraBytes используется значение по умолчанию
            PrivSigningKeyWif    = PrivateKey2Wif(potentialPrivSigningKey);
            PrivEncryptionKeyWif = PrivateKey2Wif(potentialPrivEncryptionKey);

            Status = Status.Valid;
        }
コード例 #2
0
        private Payload GetPayload()
        {
            lock (_lock4GetPayload)
                if (_payload == null)
                {
                    if (_version == 2)
                    {
                        PrivateKey privkey = PrivateKey.GetPrivateKey(_bm.DB, Key);
                        if (privkey == null)
                        {
                            throw new Exception("PrivateKey not found");
                        }

                        MemoryStream payload = new MemoryStream(1000 + Subject.Length + Body.Length);                 // TODO realy 1000?
                        Random       rnd     = new Random();
                        ulong        dt      = DateTime.UtcNow.ToUnix() + (ulong)rnd.Next(600) - 300;

                        payload.Write(dt);
                        payload.WriteVarInt(Version);
                        payload.WriteVarInt(Stream);

                        MemoryStream dataToEncrypt = new MemoryStream(1000 + Subject.Length + Body.Length);                 // TODO realy 1000?
                        dataToEncrypt.WriteVarInt(Version);

                        byte[] publicAddress = privkey.GetPayload4Broadcast();
                        dataToEncrypt.Write(publicAddress, 0, publicAddress.Length);

                        Byte encodingType = (byte)EncodingType;
                        dataToEncrypt.Write(encodingType);
                        dataToEncrypt.WriteVarStr("Subject:" + Subject + "\nBody:" + Body);

                        byte[] signature = privkey.Sign(dataToEncrypt.ToArray());

                        dataToEncrypt.WriteVarInt((UInt64)signature.Length);
                        dataToEncrypt.Write(signature, 0, signature.Length);

                        var privEncryptionKey = privkey.Sha512VersionStreamHashFirst32();
                        var pubEncryptionKey  = ECDSA.PointMult(privEncryptionKey);

                        byte[] bytesToEncrypt = dataToEncrypt.ToArray();
                        byte[] encrypt        = ECDSA.Encrypt(bytesToEncrypt, pubEncryptionKey);

                        payload.Write(encrypt, 0, encrypt.Length);

                        _payload = new Payload("broadcast", ProofOfWork.AddPow(payload.ToArray()));
                    }
                }
            return(_payload);
        }