0

I need to sign a string using VB.NET. In C# the syntax would be :

string stringToSign = $"{method}\n\n\n{request.ContentLength}\n\n{request.ContentType}\n\n\n\n\n\n\n{headerResource}\n{urlResource}";
string signature = Convert.ToBase64String(hmac.ComputeHash(Encoding.UTF8.GetBytes(stringToSign )));

The $ is not allowed in VB.NET. When I attempt the same in VB.NET the checksum is different. I guess the problem is the \n, because when these are removed the sums match.I tries to escape the backslash by adding an extra \ before the \n (\\n).

I have searched but failed to find a good solution. Any ideas?

  • You can't use `\n`, `\r`, `\r\n` ...etc. in `VB.NET`. Use `Environment.NewLine` for `\n` instead. Also check `vbNewLine` and `ControlChars.NewLine`. –  Feb 22 '20 at 12:14
  • 2
    String interpolation can solve that as well, use `{vbLf}`. – Hans Passant Feb 22 '20 at 12:17
  • 3
    The $ is allowed in VB, but I think that it may have been introduced one version later than in C#. I'm pretty sure that it's been around since 2015. – jmcilhinney Feb 22 '20 at 12:26
  • Hence, you can: `Dim n = ControlChars.NewLine : Dim stringToSign = $"{method}{n}{n}{n}{request.ContentLength}{n}{n}{request.ContentType}{n}{n}{n}{n}{n}{n}{n}{headerResource}{n}{urlResource}"` or use `vbLf` instead of `ControlChars.NewLine` as mentioned. –  Feb 22 '20 at 12:44
  • Environment.NewLine does not work but Convert.ToChar(10).ToString works. Cannot find ControlChars.NewLine. What namespace does it belong to? – user3008586 Feb 22 '20 at 12:53
  • 2
    `ControlChars.NewLine` is a carriage return and a line feed, I believe. That's why it is type `String` rather than type `Char`. If you want just a line feed than use `ControlChars.Lf`, as has already been suggested. As for where it is, you don't need to ask us. Microsoft didn't spend thousands of man hours writing [documentation](https://learn.microsoft.com/en-au/dotnet/api/?view=netframework-4.8) for nothing. Even without that link, the documentation for `ControlChars` was the first result in a Bing search for "controlchars". We shouldn't have to explain how to search the web. – jmcilhinney Feb 22 '20 at 13:37
  • jmcilhinney : Sorry but I did search and cannot understand why I cannot get intelisense on the ControlChars, thus my question. Pls forgive my ignorance. – user3008586 Feb 22 '20 at 14:32
  • 1
    @user3008586 You may need to make sure that [`Option Strict On`](https://stackoverflow.com/a/29985039/1115360) is enabled (for the current project as well as being the default for new projects) so that IntelliSense can work properly. – Andrew Morton Feb 22 '20 at 14:41
  • If you don't get IntelliSense on something, it might be because you're missing an `Imports` statement on the file. In that case, the easy way to fix it is to put it in the file anyway, and then Visual Studio will give an option to fix it by adding the missing `Imports`. – Craig Feb 24 '20 at 14:22

1 Answers1

0

If your problem is linked to VB.NET interpolation, you can use $"" in VB.Net in replacing \n by {vbLf} as done in following example

Dim stringToSign As String = $"{method}{vbLf}{vbLf}{vbLf}{request.ContentLength}{vbLf}{vbLf}{request.ContentType}{vbLf}{vbLf}{vbLf}{vbLf}{vbLf}{vbLf}{vbLf}{headerResource}{vbLf}{urlResource}"

If your problem is linked to ComputeHash() or something else, you must change title of your question !

schlebe
  • 3,387
  • 5
  • 37
  • 50