password hashbytes desencriptar create sql sql-server-2008 hash

hashbytes - select sha1 sql server



SQL Server 2008 R2 HASHBYTES SHA2 devuelve nulo (5)

Estoy tratando de usar HASHBYTES con SHA2_512 como algo. Sin embargo, cuando trato de hacerlo en SQL Server Management Studio, todo lo que obtengo es nulo.

SELECT HASHBYTES(''SHA1'',''test'') //works SELECT HASHBYTES(''SHA2'',''test'') //returns null

¿Qué estoy haciendo mal?
¿Hay alguna manera de ver el valor de retorno de SELECT HASHBYTES(''SHA2'', ''test'') ?

Gracias



Aquí un pequeño ejemplo con 128, 256 y 512 Bits.

DECLARE @HashThis nvarchar(4000); SELECT @HashThis = CONVERT(nvarchar(4000),''This is a sample string''); SELECT HASHBYTES(''SHA1'', @HashThis); SELECT HASHBYTES(''SHA2_256'', @HashThis); SELECT HASHBYTES(''SHA2_512'', @HashThis); GO


Es posible devolver un hash SHA512 en SQL Server 2008 si utiliza una función definida por el usuario (UDF) en CLR. Sin incluir una explicación completa de cómo hacer CLR en SQLServer, aquí están las partes relevantes.

Primero, el código C # CLR:

using System.Text; using System.Data.SqlTypes; using Microsoft.SqlServer.Server; using System.Security.Cryptography; public partial class UserDefinedFunctions { [Microsoft.SqlServer.Server.SqlFunction(DataAccess = DataAccessKind.Read)] [return: SqlFacet(MaxSize = -1)] public static SqlString hash_string_sha512([SqlFacet(MaxSize = -1)]string Value) { SHA512Managed crypt = new SHA512Managed(); string hashString = string.Empty; byte[] crypto = crypt.ComputeHash(Encoding.UTF8.GetBytes(Value), 0, Encoding.UTF8.GetByteCount(Value)); foreach (byte bit in crypto) { hashString += bit.ToString("x2"); } return hashString; } };

Construye tu proyecto CLR, que crea una DLL. Ahora cree un conjunto en la base de datos para la DLL y registre la función:

create assembly MyCode from ''[PATH]/[DLL_Name].dll'' with permission_set = external_access create function hash_string_sha512(@val nvarchar(max)) returns nvarchar(max) as external name MyCode.UserDefinedFunctions.hash_string_sha512

Ahora puedes juntar cualquier cadena:

select dbo.hash_string_sha512(''What will this look like as a SHA512 hash?'')

Lo que devuelve el hash:

42f8373d528cb64cdfa7ec4ffb2d754c7d4c37a28959506ec2413aacfe17500db7940ffd887390cb543a8615a6000b4f6bcbd199bb56af91bec84780f236aaf8


SQL Server admite SHA2 512 en SQL Server 2012+.

SQL Server 2008 R2 y sus versiones anteriores NO son compatibles con SHA2_512. Aquí está HASHBYTES en MSDN .


SELECT HASHBYTES(''SHA2_256'',''test'') SELECT HASHBYTES(''SHA2_512'',''test'')