"Specified value has invalid Control characters" when converting SHA512 output to string

I am attempting to create an Hash for an API. my input is something like this:


And my expected output is like :


I tried the bellow but I keep getting "Specified value has invalid Control characters. Parameter name: value"

I am actually doing this in a REST service.

public static string GetHash(string text)
    string hash = "";
    SHA512 alg = SHA512.Create();
    byte[] result = alg.ComputeHash(Encoding.UTF8.GetBytes(text));
    hash = Encoding.UTF8.GetString(result);        
    return hash;

What am I missing?


The problem is Encoding.UTF8.GetString(result) as the data in result is invalid UTF-8 (it's just binary goo!) so trying to convert it to text is invalid - in general, and specifically for this input - which results in the Exception being thrown.

Instead, convert the byte[] to the hex representation of said byte sequence; don't treat it as UTF-8 encoded text.

See the questions How do you convert Byte Array to Hexadecimal String, and vice versa? and How can I convert a hex string to a byte array?, which discuss several different methods of achieving this task.

In order to make this work you need to convert the individual byte elements into a hex representation

var builder = new StringBuilder();
foreach(var b in result) {
  builder.AppendFormat("{0:X2}", b);
return builder.ToString();

You might want to consider using Base64 encoding (AKA UUEncode):

public static string GetHash(string text)
    SHA512 alg = SHA512.Create();
    byte[] result = alg.ComputeHash(Encoding.UTF8.GetBytes(text));
    return Convert.ToBase64String(result);

For your example string, the result is


It has an advantage of being more compact than encoding each byte into two characters: three bytes takes four characters with Base64 encoding or six characters the other way.

Need Your Help

Verbally format a number in Python

python formatting nlp language-comparisons

How do pythonistas print a number as words, like the equivalent of the Common Lisp code:

How to make a field in a definition required for some operations and not others

swagger swagger-2.0 swagger-editor

I'm writing my swagger definition in yaml. Say I have a definition that looks something like this.