Click here to Skip to main content
15,884,237 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am working on a project that needs to create a signature. I have a C# module that works and I am trying to create a vb.net sample but it is not generating the same value and I am not sure what I am doing wrong.

All help is welcome.

Values:

key= "dab01234dab01234"
message ="GET\napi-clientid:06f68777d07e4d2994a657834662bc345\napi-version:1\ntimestamp:4/06/2015 11:04am\ntops-clientid:dab\ncommunity"

C# Routine
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Web.Http;

private static string GetSignature(string key, string message) {
var hmac = new HMACSHA256(Encoding.UTF8.GetBytes(key));
return Convert.ToBase64String(hmac.ComputeHash(Encoding.UTF8.GetBytes(message)));
}
}
Value Returned by C#
signature "G2LWmwWsDkTbOLahA8/EykgkGIakk5kOl9ZNXrOuKAY=" string

VB.net Routine
Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Net.Http
Imports System.Net.Http.Headers
Imports System.Security.Cryptography
Imports System.Text
Imports System.Threading.Tasks
Imports System.Web.Http

Private Function GetSignature(ByVal key As String, ByVal message As String) As String

Dim hmac As HMACSHA256 = New HMACSHA256(Encoding.UTF8.GetBytes(key))
Return Convert.ToBase64String(hmac.ComputeHash(Encoding.UTF8.GetBytes(message)))

End Function

Value returned by VB
sSignature "qWrq4fx1U/JYKBI+z6yIw10+oH3x3nHJ4/JTHGIlIHA=" String
Posted
Updated 13-Apr-15 8:45am
v3

You're passing different strings to the method.
  • In a C# string literal, "\n" represents a new-line character (ASCII code 10).
  • In a VB string literal, "\n" represents a back-slash (ASCII code 92) followed by a lower-case "n" (ASCII code 110).


Either change your C# code to use a verbatim string literal (string message = @"...";), or change your VB.NET string to use embedded new-line characters (Dim message As String = "..." & Chr(10) & "..." & Chr(10) & "...").
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 13-Apr-15 15:10pm    
Aha, good catch, a 5.
—SA
Dave-10169531 13-Apr-15 15:20pm    
Thank you Richard.
There is no "convert". This is called "translation", from one language to another. There is no need to ask such questions, as you can translate everything automatically. Please see my past answer:
Code Interpretation, C# to VB.NET[^].

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900