chobo
chobo

Reputation: 32281

How to convert varbinary to GUID in TSQL stored procedure?

how can I convert the HASHBYTES return value to a GUID?

This is what I have so far.

CREATE PROCEDURE [dbo].[Login]
    @email nvarchar,
    @password varchar
AS
BEGIN
    DECLARE @passHashBinary varbinary; 
    DECLARE @newPassHashBinary varbinary;

    -- Create a unicode (utf-16) password
    Declare @unicodePassword nvarchar;
    Set @unicodePassword = CAST(@password as nvarchar); 

    SET @passHashBinary = HASHBYTES('md5', @password);
    SET @newPassHashBinary = HASHBYTES('md5', @unicodePassword);

Upvotes: 3

Views: 7847

Answers (1)

Remus Rusanu
Remus Rusanu

Reputation: 294227

Simply cast it:

select cast(hashbytes('md5','foo') as uniqueidentifier)

But there are two questions lingering:

  • why cast HASHBYTES to guid? Why not use the appropriate type for storage, namely BINARY(16)
  • I hope you are aware that MD5 hashing passwords is basically useless, right? Because of rainbow tables. You need to use a secure hashing scheme, like an HMAC or the HA1 of Digest.

Upvotes: 12

Related Questions