John Davidson

php - Different hash comes out with fn_md5

0 comments
Message:


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 :<



My Answer:
odbc_prepare($xodbc_connect, "SELECT dbo.fn_md5('test', '1') as hash");_ $result = odbc_execute($statement);_ $row = odbc_fetch_array($statement);_ echo bin2hex($row['hash']);_?>_when i run this php script, the hash that is outputted is different from the hash that is outputted when i directly call the fn_md5 function in ssms. i have checked the parameters that are being passed to the function and they are the same in both cases. i have also checked the encoding and collation settings in both ssms and php and they are the same. i am at a loss as to why this is happening. any help would be greatly appreciated.

Rate this post

5 of 5 based on 2538 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us