Jos
Jos

Reputation: 3

How to set a hash in a variable?

I am trying to encrypt the result of this query in variable, the point is that I am not getting the hash but a strange string of symbols

ALTER PROCEDURE [dbo].[EncriptDesEncriptPassword] (
    @pUsuario CHAR(15), 
    @pContraseña AS VARCHAR(10),
)
AS
BEGIN    
    DECLARE @ContraseñaHash varchar(70),
    @passwordHash varchar(70);

   SELECT @ContraseñaHash = HASHBYTES ('SHA2_256', @pContraseña) FROM UsersApp2 WHERE Usuario= @pUsuario

The expected result:
0xFEED6896B729BF4A2EA5396FF3EFCB99CEB2187CA825AC8B9598D3819D1142A5

What I am getting:
þíh–·)¿J.¥9oóï˙β|¨%¬‹•˜ÓB¥

Upvotes: 0

Views: 264

Answers (1)

Cetin Basoz
Cetin Basoz

Reputation: 23807

Instead of varchar you should have used varbinary to get such a result. ie:

ALTER PROCEDURE [dbo].[EncriptDesEncriptPassword] (
    @pUsuario CHAR(15), 
    @pContraseña AS VARCHAR(10),
     
    
       )
       AS
     BEGIN

        DECLARE
  @ContraseñaHash varbinary(1000),
    @passwordHash varchar(70);

   
   
   SELECT @ContraseñaHash = HASHBYTES ('SHA2_256', @pContraseña)
FROM UsersApp2 
WHERE Usuario= @pUsuario
--...

Upvotes: 1

Related Questions