title: Implementing SQL CHECKSUM in Java
body: <p>I have an existing database in SQL Server 2008 that performs user authentication via stored procedure for an existing PHP web application.  The web application sends the stored procedure a string, however the stored procedure stores, and checks the value with SQL Checksum (<a href="http://msdn.microsoft.com/en-us/library/ms189788.aspx" rel="nofollow">http://msdn.microsoft.com/en-us/library/ms189788.aspx</a>).  The Stored Procedure casts the string as NVARCHAR(50), and stores the CHECKSUM as int in the user table.</p>
  
  <p>I am now writing a new Java application on top of the existing database, and I'm writing a custom spring authentication manager.  I would like to re-implement the CHECKSUM algorithm in Java so I do not need to call a stored procedure to perform the conversion, however I can not find any documentation on how SQL CHECKSUM works.</p>
  
  <p>I tried the following code, with the guess that it was CRC32, however it fails to return the same value as SQL CHECKSUM:</p>
  
  <pre><code>String pass = "foobar";
  CRC32 crc32 = new CRC32();
  crc32.update(pass.getBytes("UTF-16")); //This is due to the stored procedure casting as nvarchar
  crc32.getValue();
  </code></pre>
  
  <p>Can anyone point me to the algorithm that SQL CHECKSUM uses so I can re-implement it in Java?</p>
  
  <p>The question also isn't which algorithm provides the best hash for security. Security is outside of the requirements in this particular instance, as we are not prepared to force a system wide password reset. The question is what algorithm is used by T-SQL CHECKSUM, so that it could be re-implemented. This particular use case is for auth, however there is potential for this being necessary in many different applications.</p>