Exemple #1
0
        /// <summary>
        /// The create hash.
        /// </summary>
        /// <param name="keyContext">
        /// The key context.
        /// </param>
        /// <param name="algid">
        /// The algid.
        /// </param>
        /// <param name="flags">
        /// The flags.
        /// </param>
        /// <returns>
        /// The <see cref="HashContext"/>.
        /// </returns>
        /// <exception cref="Win32Exception">
        /// </exception>
        private HashContext CreateHash(KeyContext keyContext, int algid, int flags)
        {
            var hashHandler = IntPtr.Zero;
            var keyHandler  = IntPtr.Zero;

            if (keyContext != null)
            {
                keyHandler = keyContext.Handler;
            }

            if (!CryptoApi.CryptCreateHash(cspHandler, algid, keyHandler, flags, ref hashHandler))
            {
                throw new Win32Exception();
            }

            var hashContext = new HashContext(hashHandler);

            return(hashContext);
        }
        public static void Main()
        {
            Configuration.Default.AddApiKey("api_key", "YOUR_API_KEY");

            var cryptoApi = new CryptoApi();
            var timeframe = "h1";         // string | The time interval for the prices.
            var pair      = "btcusd";     // string | Return prices for the given Crypto Currency Pair. (optional)
            var exchange  = "gemini";     // string | Return prices for a Crypto Currency on the given Crypto Exchange. (optional)
            var currency  = "BTC";        // string | Return prices for the given Crypto Currency. (optional)
            var timezone  = "UTC";        // string | Return price date/times in this timezone, also interpret start/end date/time parameters in this timezone. (optional)  (default to UTC)
            var startDate = "2018-01-01"; // string | Return Crypto Prices on or after this date. (optional)
            var startTime = "14:20:00";   // string | Return Crypto Prices at or after this time (24-hour). (optional)
            var endDate   = "2019-01-01"; // string | Return Crypto Prices on or before this date. (optional)
            var endTime   = "21:01:21";   // string | Return Crypto Prices at or before this time (24-hour). (optional)
            var pageSize  = 100;          // int? | An integer greater than or equal to 1 for specifying the number of results on each page. (optional)  (default to 100)

            try
            {
                ApiResponseCryptoPrices result          = cryptoApi.GetCryptoPrices(timeframe, pair, exchange, currency, timezone, startDate, startTime, endDate, endTime, pageSize);
                List <CryptoPrice>      prices          = result.Prices;
                CryptoPairSummary       crypto_pair     = result.Pair;
                CryptoExchangeSummary   crypto_exchange = result.Exchange;

                Console.WriteLine(prices.Count + " prices found for " + crypto_pair.Name + " on " + crypto_exchange.Name + "!");
                Console.WriteLine();

                prices.ForEach(delegate(CryptoPrice price)
                {
                    Console.WriteLine("Time:   " + price.Time);
                    Console.WriteLine("Open:   " + price.Open);
                    Console.WriteLine("High:   " + price.High);
                    Console.WriteLine("Low:    " + price.Low);
                    Console.WriteLine("Close:  " + price.Close);
                    Console.WriteLine("Volume: " + price.Volume);
                    Console.WriteLine();
                });
            }
            catch (Exception e)
            {
                Debug.Print("Exception when calling CryptoApi.GetCryptoPrices: " + e.Message);
            }
        }
Exemple #3
0
        /// <summary>
        ///     Получить сертификат для текущего ключа
        /// </summary>
        /// <returns></returns>
        public byte[] GetSertificateData()
        {
            uint gSize = 0;

            // получим размер сертификата
            if (!CryptoApi.CryptGetKeyParam(handler, Constants.KpCertificate, null, ref gSize, 0))
            {
                throw new Win32Exception(Marshal.GetLastWin32Error());
            }

            var byteCertificate = new byte[gSize];

            // и сам сертификат
            if (!CryptoApi.CryptGetKeyParam(handler, Constants.KpCertificate, byteCertificate, ref gSize, 0))
            {
                throw new Win32Exception(Marshal.GetLastWin32Error());
            }

            return(byteCertificate);
        }
Exemple #4
0
        /// <summary>
        /// The import key.
        /// </summary>
        /// <param name="protectionKeyContext">
        /// The protection key context.
        /// </param>
        /// <param name="keyData">
        /// The key data.
        /// </param>
        /// <param name="keyDataLength">
        /// The key data length.
        /// </param>
        /// <param name="flags">
        /// The flags.
        /// </param>
        /// <returns>
        /// The <see cref="KeyContext"/>.
        /// </returns>
        /// <exception cref="Win32Exception">
        /// </exception>
        private KeyContext ImportKey(KeyContext protectionKeyContext, byte[] keyData, int keyDataLength, int flags)
        {
            var protectionKeyHandler = IntPtr.Zero;

            if (protectionKeyContext != null)
            {
                protectionKeyHandler = protectionKeyContext.Handler;
            }

            var keyHandler = IntPtr.Zero;

            if (!CryptoApi.CryptImportKey(cspHandler, keyData, keyDataLength, protectionKeyHandler, flags, ref keyHandler))
            {
                throw new Win32Exception();
            }

            var keyContext = new KeyContext(keyHandler);

            return(keyContext);
        }
Exemple #5
0
        /// <summary>
        ///     Возвращает хэш.
        /// </summary>
        /// <returns>Значение хэша.</returns>
        public byte[] GetValue()
        {
            int dataLenth = 0;

            if (!CryptoApi.CryptGetHashParam(Handler, Constants.HpHashValue, null, ref dataLenth, 0))
            {
                throw new Win32Exception();
            }
            if (dataLenth == 0)
            {
                throw new Win32Exception();
            }
            var result = new byte[dataLenth];

            if (!CryptoApi.CryptGetHashParam(Handler, Constants.HpHashValue, result, ref dataLenth, 0))
            {
                throw new Win32Exception();
            }
            return(result);
        }
Exemple #6
0
        internal HashContext CreateHash(KeyContext keyContext, int flags)
        {
            int algid = ProviderParams.GetHashAlgId();

            IntPtr hashHandler = IntPtr.Zero;
            IntPtr keyHandler  = IntPtr.Zero;

            if (keyContext != null)
            {
                keyHandler = keyContext.Handler;
            }

            if (!CryptoApi.CryptCreateHash(cspHandler, algid, keyHandler, flags, ref hashHandler))
            {
                throw new Win32Exception();
            }

            var hashContext = new HashContext(hashHandler);

            return(hashContext);
        }
Exemple #7
0
        private void BtnSave_Click(object sender, EventArgs e)
        {
            string connectStr = SetConnectString();

            if (!string.IsNullOrEmpty(connectStr))
            {
                string jsonData = "[{"
                                  + "\"Name\":\"ArmyKnowledge\","
                                  + "\"ConnectionString\":\"" + CryptoApi.AesEncrypt(connectStr) + "\","//需要加密保存
                                  + "\"ProviderName\":\"System.Data.SqlClient\""
                                  + "}]";
                try
                {
                    File.WriteAllText(GetDirectory() + FileName, jsonData);
                }
                catch (Exception exp)
                {
                    MessageBox.Show(this, exp.Message, "失败提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                MessageBox.Show(this, "数据库配置保存成功!", "成功提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
        public static void Main()
        {
            Configuration.Default.AddApiKey("api_key", "YOUR_API_KEY");

            var cryptoApi = new CryptoApi();
            var pair      = "btcusd"; // string | Return the snapshot for the given Crypto Currency Pair. (optional)
            var exchange  = "gemini"; // string | Return the snapshot for a Crypto Currency on the given Crypto Exchange. (optional)
            var currency  = "BTC";    // string | Return the snapshot for the given Crypto Currency. (optional)

            try
            {
                ApiResponseCryptoSnapshot result          = cryptoApi.GetCryptoSnapshot(pair, exchange, currency);
                CryptoPairSummary         crypto_pair     = result.Pair;
                CryptoExchangeSummary     crypto_exchange = result.Exchange;
                CryptoSnapshot            snapshot        = result.Snapshot;

                Console.WriteLine("Snapshot for " + crypto_pair.Name + " on " + crypto_exchange.Name + "!");
                Console.WriteLine();
                Console.WriteLine("Last updated:     " + snapshot.LastUpdated);
                Console.WriteLine("Bid:              " + snapshot.Bid);
                Console.WriteLine("Bid size:         " + snapshot.BidSize);
                Console.WriteLine("Ask:              " + snapshot.Ask);
                Console.WriteLine("Ask size:         " + snapshot.AskSize);
                Console.WriteLine("Change:           " + snapshot.Change);
                Console.WriteLine("Change percent:   " + snapshot.ChangePercent);
                Console.WriteLine("Volume:           " + snapshot.Volume);
                Console.WriteLine("Open:             " + snapshot.Open);
                Console.WriteLine("High:             " + snapshot.High);
                Console.WriteLine("Low:              " + snapshot.Low);
                Console.WriteLine("Last trade side:  " + snapshot.LastTradeSide);
                Console.WriteLine("Last trade time:  " + snapshot.LastTradeTime);
                Console.WriteLine("Last trade price: " + snapshot.LastTradePrice);
                Console.WriteLine("Last trade size:  " + snapshot.LastTradeSize);
            }
            catch (Exception e)
            {
                Debug.Print("Exception when calling CryptoApi.GetCryptoSnapshot: " + e.Message);
            }
        }
Exemple #9
0
        public void CryptoApi_UrlTokenEncodeDecode()
        {
            for (byte size = 1; size < 20; size++)
            {
                var input = new byte[size];
                for (var i = 0; i < input.Length; i++)
                {
                    input[i] = ((i % 2) == 0) ? (byte)248 : (byte)252;
                }

                var encoded = CryptoApi.UrlTokenEncode(input);
                var output  = CryptoApi.UrlTokenDecode(encoded);

                Assert.AreEqual(HttpServerUtility.UrlTokenEncode(input), encoded);

                Assert.AreEqual(input.Length, output.Length);
                for (var i = 0; i < input.Length; i++)
                {
                    Assert.AreEqual(input[i], output[i]);
                }
            }
        }
Exemple #10
0
        private byte[] ExportKey(KeyContext protectionKeyContext, int blobType, int falgs)
        {
            IntPtr protectionKeyHandler = IntPtr.Zero;

            if (protectionKeyContext != null)
            {
                protectionKeyHandler = protectionKeyContext.Handler;
            }

            int exportBufferSize = 0;

            if (!CryptoApi.CryptExportKey(handler, protectionKeyHandler, blobType, falgs, null, ref exportBufferSize))
            {
                throw new Win32Exception();
            }

            var result = new byte[exportBufferSize];

            if (!CryptoApi.CryptExportKey(handler, protectionKeyHandler, blobType, falgs, result, ref exportBufferSize))
            {
                throw new Win32Exception();
            }
            return(result);
        }
        public SenseNet.Services.IdentityManagement.LookupIdentityResult LookupIdentity(string identityToken)
        {
            string username = string.Empty;

            try
            {
                username = CryptoApi.Decrypt(identityToken, "sensenet60", "SenseNetContentRepository");
            }
            catch //rethrow
            {
                throw new InvalidTokenException();
            }

            Debug.WriteLine("Token resolved to:" + username);
            string[] userParts = username.Split('\\');
            User     user      = null;

            try
            {
                user = User.Load(userParts[0], userParts[1]);
            }
            catch (Exception e) //rethrow
            {
                throw new SecurityException(e.Message, e);
            }

            if (user == null)
            {
                throw new InvalidParameterException(username);
            }

            return(new LookupIdentityResult()
            {
                Identity = new Identity(user),
            });
        }
Exemple #12
0
 public void Init()
 {
     instance = new CryptoApi();
 }