'Different hash comes out with fn_md5

so i have a very weird issue that its been quite sometime now and am still figuring out why it is is. i have a function in sql server called fn_md5 that accepts 2 parameters and then return it as a varbinary.

but if the function is called via a the php script in my web, the hash is different when you directly call it from the ssms, and all of my ideas are out anymore. all i know is that the fn_md5 function when called directly in the ssms outputs the correct hash that i want it to be.

soo this is my fn_md5 function:

    USE [master]
GO
/****** Object:  UserDefinedFunction [dbo].[UFN_MD5_ENCODEVALUE]    Script Date: 5/17/2022 4:41:16 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

-- ЗФјцён : UFN_MD5_ENCODEVALUE()
-- і»їл : ЖЇБ¤№®АЪї­°ъ АОµ¦Ѕєё¦ АМїлЗПї© MD5 °ЄА» »эјє
ALTER FUNCTION [dbo].[fn_md5]
(
    @btInStr        VARCHAR(10),
    @btInStrIndex       VARCHAR(10)
)
RETURNS VARBINARY(16)
AS  
BEGIN 
    DECLARE @btOutVal   VARBINARY(16)

    EXEC master..XP_MD5_EncodeKeyVal @btInStr, @btInStrIndex, @btOutVal OUT

    RETURN  @btOutVal
END

and this is my php script:

$xodbc_connect = odbc_connect('Driver={ODBC Driver 17 for SQL Server};SERVER=WIN-6QBM0ALD4G1\SQLEXPRESS', 'sa', 'superpasszz');
        $statement = "select [dbo].fn_md5('passpass', 'useruser')";
        $exec = odbc_exec($xodbc_connect, $statement);
        $result = odbc_result($exec, 1);
        $password = $result;

        //var_dump($password);

        # insert data
        $data = array(
            'username' => $username,
            'password' => $password,
            'name' => $username,
            'serial' => $this->_defaultAccountSerial,
            'email' => $email
        );
        
        # query
            $query = "INSERT INTO "._TBL_MI_." ("._CLMN_USERNM_.", "._CLMN_PASSWD_.", "._CLMN_MEMBNAME_.", "._CLMN_SNONUMBER_.", "._CLMN_EMAIL_.", "._CLMN_BLOCCODE_.", "._CLMN_CTLCODE_.") VALUES (:username, :password, :name, :serial, :email, 0, 0)";

now if i do this script in the ssms:

select [dbo].fn_md5('passpass', 'useruser');

it will give me this, which should be the correct hash:

0x0634DF7B99E2CF2344C3362F8CB76729

but if i use the registration process via my web it will give me this instead:

0xE5E892FA86C13BB1DF93630458E7DC31

the more weirder is that i cannot see what is wrongg and my head is going to blow. am still constantly learning both languages please bare with me and i hope you can help me out :<



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source