public void Authenticate(IRestClient client, IRestRequest request) { var timestamp = Nonce.ToString(); var body = GetBody(request); // CB-ACCESS-KEY The api key as a string. request.AddHeader(_cbAccessKeyHeader, _key); // CB-ACCESS-SIGN The base64-encoded signature (see Signing a Message). //var what = timestamp + method + requestPath + body; var combined = timestamp + (request.Method == Method.POST ? "POST" : "GET") + "/" + request.Resource + body; var hashed = _hash.ComputeHash(Encoding.UTF8.GetBytes(combined)); var signed64 = Convert.ToBase64String(hashed); request.AddHeader(_cbAccessSign, signed64); // CB-ACCESS-TIMESTAMP A timestamp for your request. request.AddHeader(_cbAccessTimeStampHeader, timestamp); // CB-ACCESS-PASSPHRASE The passphrase you specified when creating the API key. request.AddHeader(_cbAcccessPass, _passphrase); // All request bodies should have content type application/json and be valid JSON. }
public void ToString_() { var nonce = new Nonce( new byte[] { 0x00, 0x01, 0x80, 0xff } ); Assert.Equal("000180ff", nonce.ToString()); }
public string SignatureInputString() { return(String.Join( SEPARATOR, new string[] { Sender, Nonce.ToString(), Operation.ToSerializedString(), Recipient, Timestamp.ToString(), PublicKey })); }
public byte[] GetBlockData() { string data = Number.ToString(); data += Nonce.ToString(); data += PreviousHash?.ToString(); data += Data?.ToString(); return(Encoding.ASCII.GetBytes(data)); }
public JObject ToJson() { JObject json = new JObject(); json["topPort"] = TcpPort.ToString(); json["wsPort"] = WsPort.ToString(); json["nonce"] = Nonce.ToString(); json["useragent"] = UserAgent; return(json); }
public static void Ctor() { var actual = new Nonce(); Assert.Equal(0, actual.Size); Assert.Equal(0, actual.FixedFieldSize); Assert.Equal(0, actual.CounterFieldSize); Assert.Equal(new byte[0], actual.ToArray()); Assert.Equal("[][]", actual.ToString()); actual.CopyTo(Span <byte> .Empty); }
public override async Task <string> CanselOrder(Order order) { string url = "https://poloniex.com/tradingApi"; var postPars = new Dictionary <string, string>(); postPars.Add("command", "cancelOrder"); postPars.Add("orderNumber", order.Id.ToString()); postPars.Add("nonce", Nonce.ToString()); string result = await SendPrivateApiRequestAsync(url, postPars); return(result); }
public override async Task <string> Sell(Order order) { string url = "https://poloniex.com/tradingApi"; var postPars = new Dictionary <string, string>(); postPars.Add("command", "sell"); postPars.Add("currencyPair", order.Pair); postPars.Add("rate", order.Price.ToString(CultureInfo.InvariantCulture)); postPars.Add("amount", order.Amount.ToString(CultureInfo.InvariantCulture)); postPars.Add("nonce", Nonce.ToString()); string result = await SendPrivateApiRequestAsync(url, postPars); return(result); }
public static void CtorWithCounterSize(int size) { var expected = new byte[size]; var actual = new Nonce(0, size); var array = new byte[expected.Length]; Assert.Equal(expected.Length, actual.Size); Assert.Equal(0, actual.FixedFieldSize); Assert.Equal(expected.Length, actual.CounterFieldSize); Assert.Equal(expected, actual.ToArray()); Assert.Equal("[][" + expected.EncodeHex() + "]", actual.ToString()); actual.CopyTo(array); Assert.Equal(expected, array); }
public override string ToString() { string updatedAddresses = string.Join( string.Empty, UpdatedAddresses.Select(a => "\n " + ByteUtil.Hex(a.ToArray())) ); return($@"{nameof(RawTransaction)} {nameof(Nonce)} = {Nonce.ToString(CultureInfo.InvariantCulture)} {nameof(Signer)} = {ByteUtil.Hex(Signer.ToArray())} {nameof(PublicKey)} = {ByteUtil.Hex(PublicKey.ToArray())} {nameof(UpdatedAddresses)} = {updatedAddresses} {nameof(Timestamp)} = {Timestamp} {nameof(Signature)} = {ByteUtil.Hex(Signature.ToArray())}"); }
public override async Task <string> TradeHistory(string pair, DatePeriod period) { ulong fromStamp = Utils.DateTimeToUnixTimeStamp(period.From); ulong toStamp = Utils.DateTimeToUnixTimeStamp(period.To); string url = "https://poloniex.com/tradingApi"; var postPars = new Dictionary <string, string>(); postPars.Add("command", "returnTradeHistory"); postPars.Add("currencyPair", pair); postPars.Add("start", fromStamp.ToString()); postPars.Add("end", toStamp.ToString()); postPars.Add("nonce", Nonce.ToString()); string result = await SendPrivateApiRequestAsync(url, postPars); return(result); }
public static void CtorWithFixedAndCounterSize(int size) { var fixedField = Utilities.RandomBytes.Slice(0, size / 2).ToArray(); var counterField = new byte[size - fixedField.Length]; var expected = new byte[size]; var actual = new Nonce(fixedField, counterField.Length); var array = new byte[expected.Length]; fixedField.CopyTo(expected, 0); Assert.Equal(expected.Length, actual.Size); Assert.Equal(fixedField.Length, actual.FixedFieldSize); Assert.Equal(counterField.Length, actual.CounterFieldSize); Assert.Equal(expected, actual.ToArray()); Assert.Equal("[" + fixedField.EncodeHex() + "][" + counterField.EncodeHex() + "]", actual.ToString()); actual.CopyTo(array); Assert.Equal(expected, array); }
public bool Verify() { string head = string.Empty; Enumerable.Range(1, Difficulty).ToList().ForEach(a => head += "0"); if (!Hash.StartsWith(head)) { return(false); } using (var sha256 = new SHA256Managed()) { if (!Hash.Equals( Convert.ToBase64String( sha256.ComputeHash( Encoding.Unicode.GetBytes(Nonce.ToString() + GetMessage()))))) { return(false); } } var tx = Txs.Skip(1).ToList(); var isVerified = true; tx.ForEach(a => { if (!a.Verify()) { isVerified = false; } }); if (!isVerified) { return(false); } // 同一Inputがないか? var dic = new Dictionary <string, HashSet <string> >(); tx.ForEach(a => a.Inputs.ForEach(b => { if (!dic.ContainsKey(b.PublicKey)) { dic.Add(b.PublicKey, new HashSet <string>()); } if (dic[b.PublicKey].Contains(b.TxHash)) { isVerified = false; } else { dic[b.PublicKey].Add(b.TxHash); } })); if (!isVerified) { return(false); } //generation transaction var txGen = Txs.First(); if (!txGen.Outputs.Sum(d => d.Amount) .Equals(tx.Sum(a => a.Inputs.Sum(b => b.Amount) - a.Outputs.Sum(c => c.Amount)) + 50m)) { return(false); } return(true); }
public byte[] CalculateHash() { using (var alg = SHA256.Create()){ byte[] hash = null; while (true) { hash = alg.ComputeHash(ASCII.GetBytes(Data + TimeStamp.ToString() + Nonce.ToString())); if (!MatchesBlockChainCriteria(hash)) { Nonce++; } else { break; } } Console.WriteLine($"Mined a block!!!!: {HashToString(hash)}"); return(hash); } }
private void ReHash() { Hash = Hasher.HashGen(ID, Nonce.ToString(), Data, PreviousHash); PropertyChanged(this, new PropertyChangedEventArgs("IsSigned")); }
public string CalculateHash() => HashUtil.Sha256(CreatingTime.ToString(CultureInfo.InvariantCulture), JsonConvert.SerializeObject(Data), PreviousHash, Nonce.ToString());
public string CalculateHash() { var hasher = new SHA256Managed(); var bytes = Encoding.UTF8.GetBytes(Index + TimeStamp.ToString() + Data + PreviousHash + Nonce.ToString()); var hash = hasher.ComputeHash(bytes); string result = string.Empty; foreach (var b in hash) { result += String.Format("{0:x2}", b); } return(result); }
public void OnPost() { String PrivateKeyString = HttpContext.Session.GetString("PrivateKeyString"); String ID = HttpContext.Session.GetString("Chat_ID"); String Current_User = HttpContext.Session.GetString("User_Name"); String Chat_Message = Request.Form["Chat_Message"]; String Exception = ""; Boolean CheckConnection = MyOwnMySQLConnectionClass.LoadConnection(ref Exception); MySqlCommand MySQLQuery = new MySqlCommand(); MySqlDataReader PublicKeyStringReader; MySqlDataReader RecordReader; String PublicKeyString = ""; BigInteger PrivateKey = 0; BigInteger Nonce = 0; BigInteger PublicKey = 0; BigInteger MessageInt = 0; BigInteger SaltInt = 0; Byte[] NonceByte = new Byte[] { }; Byte[] PrivateKeyByte = new Byte[] { }; Byte[] PublicKeyByte = new Byte[] { }; Byte[] SharedSecretByte = new Byte[] { }; Byte[] MessageByte = new Byte[] { }; Byte[] SaltByte = new Byte[] { }; Byte[] NewKeyByte = new Byte[] { }; int Checker = 0; int Count = 1; long OUTPUT_LENGTH = 32; if (Chat_Message != null) { MySQLQuery.CommandText = "SELECT COUNT(*) FROM `DF_Public_Key` WHERE `Requestor_1`=@Current_User AND `ID`=@ID"; MySQLQuery.Parameters.Add("@ID", MySqlDbType.Text).Value = ID; MySQLQuery.Parameters.Add("@Current_User", MySqlDbType.Text).Value = Current_User; MySQLQuery.Connection = MyOwnMySQLConnectionClass.MyMySQLConnection; MySQLQuery.Prepare(); Checker = int.Parse(MySQLQuery.ExecuteScalar().ToString()); if (Checker == 1) { MySQLQuery = new MySqlCommand(); MySQLQuery.CommandText = "SELECT `Requestor_2_PK` FROM `DF_Public_Key` WHERE `Requestor_1`=@Current_User AND `ID`=@ID"; MySQLQuery.Parameters.Add("@ID", MySqlDbType.Text).Value = ID; MySQLQuery.Parameters.Add("@Current_User", MySqlDbType.Text).Value = Current_User; MySQLQuery.Connection = MyOwnMySQLConnectionClass.MyMySQLConnection; MySQLQuery.Prepare(); PublicKeyStringReader = MySQLQuery.ExecuteReader(); while (PublicKeyStringReader.Read()) { PublicKeyString = PublicKeyStringReader.GetValue(0).ToString(); } MyOwnMySQLConnectionClass.MyMySQLConnection.Close(); } else { MySQLQuery = new MySqlCommand(); MySQLQuery.CommandText = "SELECT `Requestor_1_PK` FROM `DF_Public_Key` WHERE `Requestor_2`=@Current_User AND `ID`=@ID"; MySQLQuery.Parameters.Add("@ID", MySqlDbType.Text).Value = ID; MySQLQuery.Parameters.Add("@Current_User", MySqlDbType.Text).Value = Current_User; MySQLQuery.Connection = MyOwnMySQLConnectionClass.MyMySQLConnection; MySQLQuery.Prepare(); PublicKeyStringReader = MySQLQuery.ExecuteReader(); while (PublicKeyStringReader.Read()) { PublicKeyString = PublicKeyStringReader.GetValue(0).ToString(); } MyOwnMySQLConnectionClass.MyMySQLConnection.Close(); } CheckConnection = MyOwnMySQLConnectionClass.LoadConnection(ref Exception); PublicKey = BigInteger.Parse(PublicKeyString); PublicKeyByte = PublicKey.ToByteArray(); PrivateKey = BigInteger.Parse(PrivateKeyString); PrivateKeyByte = PrivateKey.ToByteArray(); SharedSecretByte = ScalarMult.Mult(PrivateKeyByte, PublicKeyByte); MySQLQuery = new MySqlCommand(); Checker = 0; MySQLQuery.CommandText = "SELECT COUNT(*) FROM `Chat_Message` WHERE `FK_ID`=@ID"; MySQLQuery.Parameters.Add("@ID", MySqlDbType.Text).Value = ID; MySQLQuery.Connection = MyOwnMySQLConnectionClass.MyMySQLConnection; MySQLQuery.Prepare(); Checker = int.Parse(MySQLQuery.ExecuteScalar().ToString()); if (Checker != 0) { MySQLQuery = new MySqlCommand(); MySQLQuery.CommandText = "SELECT `Salt` FROM `Chat_Message` WHERE `FK_ID`=@ID"; MySQLQuery.Parameters.Add("@ID", MySqlDbType.Text).Value = ID; MySQLQuery.Connection = MyOwnMySQLConnectionClass.MyMySQLConnection; MySQLQuery.Prepare(); RecordReader = MySQLQuery.ExecuteReader(); while (RecordReader.Read()) { SaltInt = BigInteger.Parse(RecordReader.GetValue(0).ToString()); SaltByte = SaltInt.ToByteArray(); if (Count == 1) { NewKeyByte = PasswordHash.ArgonHashBinary(SharedSecretByte, SaltByte, PasswordHash.StrengthArgon.Medium, OUTPUT_LENGTH, PasswordHash.ArgonAlgorithm.Argon_2ID13); } else { NewKeyByte = PasswordHash.ArgonHashBinary(NewKeyByte, SaltByte, PasswordHash.StrengthArgon.Medium, OUTPUT_LENGTH, PasswordHash.ArgonAlgorithm.Argon_2ID13); } Count += 1; } } if (NewKeyByte.Length == 0) { SaltByte = PasswordHash.ArgonGenerateSalt(); NewKeyByte = PasswordHash.ArgonHashBinary(SharedSecretByte, SaltByte, PasswordHash.StrengthArgon.Medium, OUTPUT_LENGTH, PasswordHash.ArgonAlgorithm.Argon_2ID13); SaltInt = new BigInteger(SaltByte); NonceByte = SecretBox.GenerateNonce(); MessageByte = SecretBox.Create(Encoding.UTF8.GetBytes(Chat_Message), NonceByte, NewKeyByte); MessageInt = new BigInteger(MessageByte); Nonce = new BigInteger(NonceByte); } else { SaltByte = PasswordHash.ArgonGenerateSalt(); NewKeyByte = PasswordHash.ArgonHashBinary(NewKeyByte, SaltByte, PasswordHash.StrengthArgon.Medium, OUTPUT_LENGTH, PasswordHash.ArgonAlgorithm.Argon_2ID13); SaltInt = new BigInteger(SaltByte); NonceByte = SecretBox.GenerateNonce(); MessageByte = SecretBox.Create(Encoding.UTF8.GetBytes(Chat_Message), NonceByte, NewKeyByte); MessageInt = new BigInteger(MessageByte); Nonce = new BigInteger(NonceByte); } MyOwnMySQLConnectionClass.MyMySQLConnection.Close(); CheckConnection = MyOwnMySQLConnectionClass.LoadConnection(ref Exception); MySQLQuery = new MySqlCommand(); MySQLQuery.CommandText = "INSERT INTO `Chat_Message`(`FK_ID`,`Message`,`Sender_Name`,`Receiver_Status`,`Salt`,`Nonce`) VALUES (@FK_ID,@Message,@Sender_Name,@Receiver_Status,@Salt,@Nonce)"; MySQLQuery.Parameters.Add("@FK_ID", MySqlDbType.Text).Value = ID; MySQLQuery.Parameters.Add("@Message", MySqlDbType.Text).Value = MessageInt.ToString(); MySQLQuery.Parameters.Add("@Sender_Name", MySqlDbType.Text).Value = Current_User; MySQLQuery.Parameters.Add("@Receiver_Status", MySqlDbType.Text).Value = "Sent"; MySQLQuery.Parameters.Add("@Salt", MySqlDbType.Text).Value = SaltInt.ToString(); MySQLQuery.Parameters.Add("@Nonce", MySqlDbType.Text).Value = Nonce.ToString(); MySQLQuery.Connection = MyOwnMySQLConnectionClass.MyMySQLConnection; MySQLQuery.Prepare(); MySQLQuery.ExecuteNonQuery(); MyOwnMySQLConnectionClass.MyMySQLConnection.Close(); Determiner = 0; if (Determiner == 0 && ConfirmationTimer == null) { SetConfirmationTimer(); } } }