﻿SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

IF OBJECT_ID('dbo.sp_GenerateNewSHA1PasswordUserFromString') IS NOT NULL 
BEGIN
  DROP PROCEDURE [dbo].[sp_GenerateNewSHA1PasswordUserFromString]
END  
GO

CREATE PROCEDURE [dbo].[sp_GenerateNewSHA1PasswordUserFromString]
	@Id int,
  @PlaintTextPassword AS NVARCHAR(MAX)
AS
BEGIN

  UPDATE fb
	  SET
		  fb.C_JELSZO = src.pwValue,
		  fb.C_SO = src.randomValue
  FROM [dbo].[T_FELHASZNALOBELEPES] AS fb 
  INNER JOIN (SELECT
		  felhasznalobelepes.ID,
		  dbo.fnConvertBase64(HASHBYTES('SHA1', CONVERT(VARBINARY(MAX), CONVERT(NVARCHAR(MAX), @PlaintTextPassword+RND)))) AS pwValue
		  ,RND AS randomValue
	  FROM 
		  (SELECT
			  ID,
			  CAST((SELECT * FROM dbo.getRand) AS nvarchar(64)) AS RND
		  FROM dbo.T_FELHASZNALOBELEPES
		  WHERE
			  ID = @Id) AS felhasznalobelepes) src ON fb.ID = src.ID

END