Exemplo n.º 1
0
        public async Task CreateNewAccount()
        {
            JObject jsonContent = null;

            try
            {
                var body = await this.Request.Content.ReadAsStringAsync();

                jsonContent = JObject.Parse(body);
            }
            catch (JsonReaderException)
            {
                CreateErrorResponse(HttpStatusCode.BadRequest, "Reading JSON Exception");
            }


            if (!(jsonContent["accountKey"] is JValue) && !(jsonContent["login"] is JValue) && !(jsonContent["password"] is JValue))
            {
                CreateErrorResponse(HttpStatusCode.BadRequest, "Bad json");
            }


            //var key = KeyParser((string)jsonContent["accountKey"]);

            //Account account = work.Accounts.GetAccount(key);
            //if (account == null)
            //{
            //    BadRequest();
            //}

            //if ( account.Role != UserRole.Admin)
            //{
            //    CreateErrorResponse(HttpStatusCode.Unauthorized, "Need admin permissions ");
            //}
            AccountKeyBuilder keyBuilder = new AccountKeyBuilder();
            var     password             = (string)jsonContent["password"];
            Account newAcc;

            using (var hasher = SHA256.Create())
            {
                newAcc = new Account()
                {
                    Key      = keyBuilder.CreateAccKey(),
                    Login    = (string)jsonContent["login"],
                    Password = new HexString(hasher.ComputeHash
                                                 (DataProvider.Serializer.ToBinaryArray(password))),
                    Role = UserRole.Admin
                };
            }
            BinaryFormatter formatter = new BinaryFormatter();
            MemoryStream    stream    = new MemoryStream();

            formatter.Serialize(stream, newAcc);
            Record record = new Record(1, "New acc " + newAcc.Login,
                                       new HexString(stream.ToArray()), TypeData.Account);
            var pub_key = new ECKeyValidator().RSA.ToXmlString(false);

            new ECKeyValidator().RSA.PersistKeyInCsp = true;
            await work.TransactionValidator.ValidateTransaction(record, pub_key);
        }
Exemplo n.º 2
0
        public void TestSerialize()
        {
            ECKeyValidator    keyValidator = new ECKeyValidator();
            var               priv_key     = keyValidator.RSA.ToXmlString(true);
            var               pub_key      = keyValidator.RSA.ToXmlString(false);
            var               sign         = keyValidator.SignData("messadhgdhdhh");
            SignatureEvidence rawSign      = new SignatureEvidence(new HexString(Serializer.ToBinaryArray(sign)),
                                                                   new HexString(Serializer.ToBinaryArray(pub_key)));
            ITransactionRepository subscr    = new TransactionRepository();
            TransactionValidator   validator = new TransactionValidator();
            var byteSign = validator.SerializeSignature(rawSign);

            byte[] bytePrivKey = null, bytePubKey = null;
            using (MemoryStream stream1 = new MemoryStream(byteSign))
            {
                using (BinaryReader reader = new BinaryReader(stream1)) {
                    reader.BaseStream.Seek(0, SeekOrigin.Begin);
                    bytePubKey  = reader.ReadBytes(243);
                    bytePrivKey = reader.ReadBytes(178);
                }
            }

            var pub  = Serializer.ToBinaryArray(pub_key);
            var priv = Serializer.ToBinaryArray(sign);

            Assert.ReferenceEquals(pub, bytePubKey);
        }
Exemplo n.º 3
0
        public void TestECK()
        {
            ECKeyValidator key        = new ECKeyValidator();
            string         publicKey  = key.RSA.ToXmlString(false);
            string         privateKey = key.RSA.ToXmlString(true);

            string plainText = "originalMessage";
            // string tamperMessage = "origiinalMessage";

            string signedMessage = key.SignData(plainText);

            Assert.IsTrue(key.VerifyMessage(plainText, signedMessage, publicKey));
        }
Exemplo n.º 4
0
        public void TestValidateTransactions()
        {
            TransactionValidator validator = new TransactionValidator();
            string dataString = "String";

            byte[]            data       = Serializer.ToHexString(dataString);
            var               message    = "Peace!!!!";
            AccountKeyBuilder keyBuilder = new AccountKeyBuilder();
            Account           newAcc;

            using (var hasher = SHA256.Create())
            {
                newAcc = new Account()
                {
                    Key      = keyBuilder.CreateAccKey(),
                    Login    = message,
                    Password = new HexString(hasher.ComputeHash
                                                 (Serializer.ToBinaryArray("212121"))),
                    Role = UserRole.Admin
                };
            }
            BinaryFormatter formatter = new BinaryFormatter();
            MemoryStream    stream    = new MemoryStream();

            formatter.Serialize(stream, newAcc);
            Record record = new Record(1, "New acc " + newAcc.Login,
                                       new HexString(stream.ToArray()), TypeData.Account);

            Record records = new Record(1, message, new HexString(data), TypeData.Host);

            var jArray = JArray.FromObject(new List <Record>()
            {
                records
            });
            // string privkey = new ECKeyValidator().CreateKeys().ToXmlString(true);
            string pubkey = new ECKeyValidator().RSA.ToXmlString(false);

            validator.ValidateTransaction(record, pubkey).Wait();
        }