简体   繁体   English

SHA签名(特殊字符)

[英]SHA Signature with special characters

I am creating a SHA signature to upload a video to the JWPlayer platform. 我正在创建SHA签名,以将视频上传到JWPlayer平台。 I am able to create the SHA signature also with special characters but when I upload the video using the signature the response from the JWPlayer platform site is "Unicode error during signature" but only if I use special characters. 我也可以使用特殊字符创建SHA签名,但是当我使用签名上传视频时,JWPlayer平台站点的响应是“签名期间出现Unicode错误”,但前提是我使用特殊字符。 It works fine if I don´t include special characters. 如果我不包含特殊字符,则效果很好。 Here is the link how to create the signature https://developer.jwplayer.com/jw-platform/reference/v1/authentication.html I have the following code to create the signature, where title can contain special character: 这是如何创建签名的链接https://developer.jwplayer.com/jw-platform/reference/v1/authentication.html我有以下代码创建签名,其中标题可以包含特殊字符:

var titleParam = $"title={title}";
var key = $"{apiFormat}&{apiKey}&{apiNonce}&{apiTimestamp}&{titleParam}{secretKey}";
StringBuilder sb;
using (var sha1 = new SHA1Managed())
{
    var hash = sha1.ComputeHash(Encoding.UTF8.GetBytes(key));
    sb = new StringBuilder(hash.Length * 2);
    foreach (byte b in hash)
     {
         sb.Append(b.ToString("x2"));
     }
 }

The key is also constructed based on user input and therefore it needs to support special characters. 密钥也是基于用户输入构造的,因此它需要支持特殊字符。 I also tried url encoding the key but without luck. 我也尝试对密钥进行url编码,但是没有运气。

It is not a SHA problem. 这不是SHA问题。 SHA does not care about encodings, it works with arrays of 8-bit bytes. SHA并不关心编码,它可以处理8位字节的数组。

How do you provide the key to the receiver, that is a potential encoding issue. 您如何将密钥提供给接收器,这是一个潜在的编码问题。 Different UTF formats? 不同的UTF格式?

Look at the data provided to SHA and how the hashed data is handled. 查看提供给SHA的数据以及如何处理散列数据。

The output of SHA is not a UTF-8 encodable data nor really any character encoding. SHA的输出不是UTF-8可编码数据,也不是任何字符编码。 It may need to be encoded into an ASCII format such as Base64 or Hexadecimal. 可能需要将其编码为ASCII格式,例如Base64或Hexadecimal。

Works correctly with Encoding.UTF8 . Encoding.UTF8正常工作。 See for example https://ideone.com/VFzogL . 参见例如https://ideone.com/VFzogL You wrote exactly as I would have written it. 你写的和我写的完全一样。

And nothing in the code you wrote can throw a "Unicode error during signature" error. 并且您编写的代码中没有任何内容会引发“签名时出现Unicode错误”错误。

Don't use Encoding.ASCII . 不要使用Encoding.ASCII It wouldn't support any non-ASCII character. 它不支持任何非ASCII字符。

The problem was with the encoding of the space character so I had to replace all + signs with %20. 问题在于空格字符的编码,因此我不得不将所有+符号替换为%20。

string EncodeParam(string param)
{
     var s = WebUtility.UrlEncode(param);
     return s.Replace("+", "%20");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM