简体   繁体   中英

SHA Signature with special characters

I am creating a SHA signature to upload a video to the JWPlayer platform. 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. 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:

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.

It is not a SHA problem. SHA does not care about encodings, it works with arrays of 8-bit bytes.

How do you provide the key to the receiver, that is a potential encoding issue. Different UTF formats?

Look at the data provided to SHA and how the hashed data is handled.

The output of SHA is not a UTF-8 encodable data nor really any character encoding. It may need to be encoded into an ASCII format such as Base64 or Hexadecimal.

Works correctly with Encoding.UTF8 . See for example 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.

Don't use Encoding.ASCII . It wouldn't support any non-ASCII character.

The problem was with the encoding of the space character so I had to replace all + signs with %20.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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