Sign() public method

public Sign ( byte data ) : byte[]
data byte
return byte[]
Esempio n. 1
0
        private Payload GetPayload()
        {
            lock (_lock4GetPayload)
                if (_payload == null)
                {
                    PrivateKey myPrivkeyFrom = PrivateKey.GetPrivateKey(_bm.DB, KeyFrom);
                    if (myPrivkeyFrom == null)
                    {
                        throw new Exception("PrivateKey not found");
                    }

                    Pubkey pubkeyTo = Pubkey.Find(_bm.DB, KeyTo);                     // TODO Получать ключ, если его ещё нет
                    if (pubkeyTo == null)
                    {
                        throw new Exception("Pubkey not found");
                    }

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

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

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

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

                    dataToEncrypt.Write(pubkeyTo.Hash, 0, 20);

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

                    byte[] askMsg = PayloadOfAskData().GetFullMsg();
                    dataToEncrypt.WriteVarInt((UInt64)askMsg.Length);
                    dataToEncrypt.Write(askMsg, 0, askMsg.Length);

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

                    //Debug.WriteLine("data=" + dataToEncrypt.ToArray().ToHex());
                    //Debug.WriteLine("SigningKey=" + myPrivkeyFrom.SigningKey.ToHex());
                    //Debug.WriteLine("signature=" + signature.ToHex());

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

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

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

                    _payload = new Payload("msg", ProofOfWork.AddPow(payload.ToArray()));
                }
            return(_payload);
        }
Esempio n. 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);
        }