Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6957613
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T15:01:38+00:00 2026-05-27T15:01:38+00:00

I am modifying an application that uses the PWDEncrypt and PWDCompare functions of SQL

  • 0

I am modifying an application that uses the PWDEncrypt and PWDCompare functions of SQL Server to store hashed passwords into a varbinary column of a table.

As these functions should not be used (due to possible changes in the algorithm or they become deprecated in the future) I would like to convert the application to use the supported HASHBYTES function instead.

Does anyone have the code to implement the same functionality as PWDENCRYPT/PWDCOMPARE using the HASHBYTES functionality so that I do not have to get every user to change their password during the change over and the existing hashed passwords remain valid?

I am using SQL Server 2008 R2.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T15:01:39+00:00Added an answer on May 27, 2026 at 3:01 pm

    I have managed to produce my own implementation of PWDENCRYPT and PWDCOMPARE using just SQL.

    Unfortunately, due to the use of RAND() in the PWDENCRYPT section you cannot convert this to a UDF.

    This is the code that performs the PWDEncrypt

    DECLARE @passwordToEncrypt NVARCHAR(MAX)
    SET @passwordToEncrypt = 'Password'
    
    DECLARE @salt VARBINARY(4)
    SET @salt = CONVERT(VARBINARY(4), RAND())
    
    DECLARE @encryptedPassword VARBINARY(128)
    
    SET
        @encryptedPassword
        =
        0x0100
        +
        --Salt
        @salt
        +
        --Hash of Password + Salt
        HASHBYTES('SHA1', @passwordToEncrypt + CONVERT(NVARCHAR(MAX), @salt))
    
    SELECT
        @passwordToEncrypt [OriginalPassword],
        @encryptedPassword [MyPWDEncrypt],
        PWDCOMPARE(@passwordToEncrypt, @encryptedPassword) [SQLPWDCompare]
    

    This is the code that performs the PWDCompare

    DECLARE @passwordToTest NVARCHAR(MAX)
    SET @passwordToTest = 'Password'
    
    DECLARE @encryptedPassword VARBINARY(128)
    SET @encryptedPassword = PWDENCRYPT(@passwordToTest)
    
    SELECT
        @passwordToTest [OriginalPassword],
        @encryptedPassword [SQLPWDEncrypt],
        CASE WHEN
            @encryptedPassword
            =
            --Header
            0x0100
            +
            --Salt
            CONVERT(VARBINARY(4), SUBSTRING(CONVERT(NVARCHAR(MAX), @encryptedPassword), 2, 2))
            +
            --Hash of Password + Salt
            HASHBYTES('SHA1', @passwordToTest + SUBSTRING(CONVERT(NVARCHAR(MAX), @encryptedPassword), 2, 2))
        THEN 1 ELSE 0 END [MyPWDCompare]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My application uses Hibernate to connect to SQL Server. I recently changed my DAO
i'm currently modifying a legacy application that uses the SqlHelper class in c#. how
I have an application that is modifying 5 identical xml files, each located on
I have simple WinForms application where modifying Windows Registry. The problem is that in
We all know that modifying a .NET web application's web.config file restarts the app
I have an application that uses coldfusion's session management (instead of the J2EE) session
How to log, trace or get queries that an application send to Microsoft SQL
I am writing a flex application that involves modifying a textarea very frequently. I
I have a command line application that runs on a windows server. The command
We have a web application that uses Spring (3.0.5) and CXF (currently 2.4.2 for

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.