Tag: hmac

基于SHA512的具有HMACfunction的FIPSvalidation应用程序?

我正在构建一个FIPSvalidation的应用程序,并在我的计算机上打开FIPS模式。 我需要HMACfunction,希望基于SHA512。 我知道HMAC SHA1function已通过FIPSvalidation,但我有一个散列函数SHA512CryptoServiceProvider,它经过FIPSvalidation,我知道FIPS实际上允许使用SHA512。 在使用FIPSvalidation的HMAC SHA512的C#中是否有类似的HMACfunction?

在DelegatingHandler中,HttpContent.ReadAsByteArrayAsync()在没有错误的情况下失败

我正在尝试为API实现HMAC安全性。 一切正常,直到我尝试发布文件。 HMAC解决方案可以在这里找到 – https://github.com/gavinharriss/WebAPI.HMAC – 它是原始的一个分支,允许GET请求和POST请求。 附加文件的代码: var requestContent = new MultipartFormDataContent(); var fileContent = new ByteArrayContent(file); requestContent.Add(fileContent, “file”, filename); 如果我立即调用HttpContent.ReadAsByteArrayAsync()没有问题,字节数组是可用的。 但是,HMAC HttpClient ( HMACHttpClient )实现了DelegatingHandler ( HMACDelegatingHandler ),以便将HMAC头附加到请求。 在HMACDelegatingHandler中 ,请求作为HttpRequestMessage传递, HttpRequestMessage.Content属性在辅助器中用于构建HMAC签名。 构建签名时,从助手类调用以下代码: private static async Task ComputeHash(HttpContent httpContent) { using (var md5 = MD5.Create()) { byte[] hash = null; if (httpContent != null) […]

AES-Encrypt-then-MAC一个带.NET的大文件

我想在.NET中以最有效的方式加密大文件(比方说64 GB)。 我将如何实现这个: 创建AesManaged实例以加密文件流(读取64 GB) 将此流保存到磁盘(因为它在内存中保持较大)(写入64 GB) 创建HMACSHA512实例以计算保存文件的哈希值(读取64 GB) 使用iv将加密数据保存到磁盘(读写64 GB) 简化的C#代码: using (var aesManaged = new AesManaged()) { using (var msEncrypt = File.OpenWrite(@”C:\Temp\bigfile.bin.tmp”)) { using (var csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write)) { File.OpenRead(@”C:\Temp\bigfile.bin”).CopyTo(csEncrypt); new MemoryStream(iv).CopyTo(csEncrypt); } } } using (var hmac = new HMACSHA512(hmacKey)) { hmacHash = hmac.ComputeHash(File.OpenRead(@”C:\Temp\bigfile.bin.tmp”)); } byte[] headerBytes; using (var […]

如何创建匹配的HMAC值以validation.NET中的Shopify WebHook?

我已经设置了一个端点来接收Shopify的webhook请求。 Shopify的请求包括从共享密钥和请求正文创建的HMAC标头。 我需要在服务器上计算HMAC并将其与请求标头中的值相匹配,以确保请求是可信的。 我似乎无法在.NET中创建适当的机制来创建匹配的HMAC值。 我的算法在这一点如下: public static string CreateHash(string data) { string sharedSecretKey = “MY_KEY”; byte[] keyBytes = Encoding.UTF8.GetBytes(sharedSecretKey); byte[] dataBytes = Encoding.UTF8.GetBytes(data); //use the SHA256Managed Class to compute the hash System.Security.Cryptography.HMACSHA256 hmac = new HMACSHA256(keyBytes); byte[] hmacBytes = hmac.ComputeHash(dataBytes); //retun as base64 string. Compared with the signature passed in the header of the post […]

如何在PHP中复制此C#散列? (toByteArray(),ComputeHash())

我试图在PHP复制以下代码,它是我必须与之接口的API的示例代码(API和示例代码在C# ,我的应用程序在PHP 5.3 )。 我不是C#开发人员,所以我很难做到这一点。 // C# Code I am trying to replicate in PHP var apiTokenId = 1887; var apiToken = “E1024763-1234-5678-91E0-T32E4E7EB316”; // Used to authenticate our request by the API (which is in C#) var stringToSign = string.Empty; stringToSign += “POST”+”UserAgent”+”http://api.com/post”; // Here is the issue, How can I do the following 3 […]

HMC SHA1哈希 – C#产生与Ruby不同的哈希输出

我正在努力为我正在使用的第三方服务快速获取一个bug .Net客户端库。 原始库(可以工作)是用Ruby编写的,但它们等效的DotNet库为Ruby库产生了不同的哈希输出。 Ruby加密代码如下: def self.encrypt_string(input_string) raise Recurly::ConfigurationError.new(“Recurly gem not configured”) unless Recurly.private_key.present? digest_key = ::Digest::SHA1.digest(Recurly.private_key) sha1_hash = ::OpenSSL::Digest::Digest.new(“sha1”) ::OpenSSL::HMAC.hexdigest(sha1_hash, digest_key, input_string.to_s) end (假设)等效的C#代码是: private static string ComputePrivateHash(string dataToProtect) { if(String.IsNullOrEmpty(Configuration.RecurlySection.Current.PrivateKey)) throw new RecurlyException(“A Private Key must be configured to use the Recurly Transparent Post API.”); byte[] salt_binary = SHA1.Create().ComputeHash(Encoding.ASCII.GetBytes(dataToProtect)); string salt_hex = BitConverter.ToString(salt_binary).Replace(“-“, “”).ToLower(); […]

如何在.NET 4.5 Core中计算HMAC-SHA1身份validation代码

我目前面临一个大问题(环境:.NET 4.5 Core):我们需要使用HMAC-SHA1算法用密钥保护消息。 问题是名称空间System.Security.Cryptography的HMACSHA1类和名称空间本身在.NET 4.5 Core中不存在,此命名空间仅存在于.NET的正常版本中。 我尝试了很多方法为我们的目的找到一个等效的命名空间,但我发现的唯一的东西是Windows.Security.Cryptography ,遗憾的是它没有提供HMAC加密。 有没有人知道如何解决我们的问题,或者是否可以免费使用第三方解决方案?

在没有第三方库的情况下设置Twitter OAuth

继续获取Twitter公共时间线,json + C#,没有第三方库 我还是C#和oAuth的新手,所以如果我什么都不懂,请耐心等待 我创建了一个名为oAuthClass的C#类,这些是我目前拥有的变量: static class oAuthClass { public static void run() { int oauth_timestamp = GetTimestamp(DateTime.Now); string oauth_nonce = PseudoRandomStringUsingGUID(); string oauth_consumer_key = “consumer key here”; string oauth_signature_method = “HMAC-SHA1”; string oauth_version = “1.0”; } } 我已经阅读了OAuth签名,我现在选择使用HMAC-SHA1,我不知道如何生成签名,在阅读和查看HTTP-Encodings和Base-Strings等内容后,我也非常困惑还有什么(我根本不知道它们的意思),但我的想法是创建一个“Http-encoded”的URL,就像空格 – >“%20”一样? 总结: – 什么是基本字符串? – 我在空间 – >%20例子吗? -HMAC-SHA1涉及消息和密钥,是消费者秘密的消息吗? 消费者关键是关键吗? – 如何通过使用HMAC-SHA1算法创建签名 – 如果我设法创建签名,我如何将这些值传递给Twitter? 我可以用 […]

C#中基于HMAC的一次性密码(RFC 4226 – HOTP)

我试图围绕生成一个6位/字符非大小写敏感的一次性密码到期。 我的来源是http://tools.ietf.org/html/rfc4226#section-5 首先是参数的定义 C 8-byte counter value, the moving factor. This counter MUST be synchronized between the HOTP generator (client) and the HOTP validator (server). K shared secret between client and server; each HOTP generator has a different and unique secret K. T throttling parameter: the server will refuse connections from a user after T […]

PHP和C#HMAC SHA256

我需要在C#中转换以下php代码: $res = mac256($ent, $key); $result = encodeBase64($res); 哪里 function encodeBase64($data) { $data = base64_encode($data); return $data; } 和 function mac256($ent,$key) { $res = hash_hmac(‘sha256’, $ent, $key, true);//(PHP 5 >= 5.1.2) return $res; } 我使用以下C#代码: byte[] res = HashHMAC(ent, key); string result = System.Convert.ToBase64String(res); 哪里 public byte[] HashHMAC(string ent, byte[] key) { byte[] toEncryptArray =System.Text.Encoding.GetEncoding(28591).GetBytes(ent); […]