locked
Generate HashValue RRS feed

  • Question

  • User944339287 posted

    Hi guys.. the following is the algorithm to generate hashvalue

    merchant_hashvalue = Concatenate the values of merchant_secret, ord_mercID, ord_mercref and  ord_totalamt.

    This is Sample code in PHP provided by Client:

    echo sha1($merchant_secret . $ord_mercID . $ord_mercref . $amount);
    

    This is my way to generate Hashvalue but failed. Any idea?
    Error message: [Invalid hash key is given].

    Imports System.Security.Cryptography

        Public Function SHA1(ByVal Key As String) As String
    
            Dim objSHA1 As New SHA1CryptoServiceProvider()
    
            objSHA1.ComputeHash(System.Text.Encoding.UTF8.GetBytes(Key.ToCharArray))
    
            Dim buffer() As Byte = objSHA1.Hash
            Dim HashValue As String = System.Convert.ToBase64String(buffer)
    
            Return HashValue
    
        End Function
    
        Protected Sub btn_submit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btn_submit.Click
    
            Dim MerchantSecret As String = "99233345"
            Dim MerchantID As String = "ABC123"
            Dim OrderID As String = "50747"
            Dim Amount As String = "2500"
    
            Dim HashedStr As String = SHA1(MerchantSecret & MerchantID & OrderID & Amount)
            //Post to destination URL
        End Sub





    Monday, January 13, 2020 8:01 AM

Answers

  • User-1780421697 posted

    VB.Net 

    'Compiler version 11.0.50709.17929 for Microsoft (R) .NET Framework 4.5
    
    Imports System
    Imports System.Collections.Generic
    Imports System.Linq
    Imports System.Text.RegularExpressions
    Imports System.Security.Cryptography
    Imports System.Text
    Imports Microsoft.VisualBasic.Val
    Imports Microsoft.VisualBasic.AscW
    
    Namespace Rextester
        Public Class Program
        
            Public Shared Sub Main(ByVal args As String())
                Dim MerchantSecret As String = "99233345"
                Dim MerchantID As String = "ABC123"
                Dim OrderID As String = "50747"
                Dim Amount As String = "2500"
                Dim sb As String = getSHA1Hash(MerchantSecret & MerchantID & OrderID & Amount)
                Console.WriteLine(sb)
            End Sub
    
          
    private shared Function getSHA1Hash(ByVal strToHash As String) As String
    
        Dim sha1Obj As New Security.Cryptography.SHA1CryptoServiceProvider
        Dim bytesToHash() As Byte = System.Text.Encoding.ASCII.GetBytes(strToHash)
    
        bytesToHash = sha1Obj.ComputeHash(bytesToHash)
    
        Dim strResult As String = ""
    
        For Each b As Byte In bytesToHash
            strResult += b.ToString("x2")
        Next
    
        Return strResult
    
    End Function
    
        End Class
    End Namespace
    

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, January 13, 2020 9:20 AM

All replies

  • User-1780421697 posted

    Code in C#

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text.RegularExpressions;
    using System.Security.Cryptography;
    using System.Text;
    
    namespace Rextester
    {
        public class Program
        {
            public static void Main(string[] args)
            {
                string MerchantSecret = "99233345";
                string MerchantID = "ABC123";
                string OrderID  = "50747";
                string Amount = "2500";
    
                    string sb = Hash(MerchantSecret+MerchantID+OrderID+Amount);
                //Your code goes here
                Console.WriteLine(sb);
            }
            
            
            static string Hash(string input)
            {
                using (SHA1Managed sha1 = new SHA1Managed())
                {
                    var hash = sha1.ComputeHash(Encoding.UTF8.GetBytes(input));
                    var sb = new StringBuilder(hash.Length * 2);
    
                    foreach (byte b in hash)
                    {
                        // can be "x2" if you want lowercase
                        sb.Append(b.ToString("X2"));
                    }
    
                    return sb.ToString();
                }
            }
            
            
        }
    }

    Monday, January 13, 2020 9:13 AM
  • User-1780421697 posted

    VB.Net 

    'Compiler version 11.0.50709.17929 for Microsoft (R) .NET Framework 4.5
    
    Imports System
    Imports System.Collections.Generic
    Imports System.Linq
    Imports System.Text.RegularExpressions
    Imports System.Security.Cryptography
    Imports System.Text
    Imports Microsoft.VisualBasic.Val
    Imports Microsoft.VisualBasic.AscW
    
    Namespace Rextester
        Public Class Program
        
            Public Shared Sub Main(ByVal args As String())
                Dim MerchantSecret As String = "99233345"
                Dim MerchantID As String = "ABC123"
                Dim OrderID As String = "50747"
                Dim Amount As String = "2500"
                Dim sb As String = getSHA1Hash(MerchantSecret & MerchantID & OrderID & Amount)
                Console.WriteLine(sb)
            End Sub
    
          
    private shared Function getSHA1Hash(ByVal strToHash As String) As String
    
        Dim sha1Obj As New Security.Cryptography.SHA1CryptoServiceProvider
        Dim bytesToHash() As Byte = System.Text.Encoding.ASCII.GetBytes(strToHash)
    
        bytesToHash = sha1Obj.ComputeHash(bytesToHash)
    
        Dim strResult As String = ""
    
        For Each b As Byte In bytesToHash
            strResult += b.ToString("x2")
        Next
    
        Return strResult
    
    End Function
    
        End Class
    End Namespace
    

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, January 13, 2020 9:20 AM