OK....here is a slightly better and amended one at last...uurgh
This one takes an example key and generates a valid token for this USE :
i.e. its a serial # generator specifically for the user in question.
======================================================
IF EXISTS (SELECT name FROM sysobjects WHERE name like
'risp_generatePassword' AND Type = 'P')
drop proc risp_generatePassword
go
create procedure risp_generatePassword @UserName varchar(50),
@iPasswordLimit int = 8,
@Password varchar(50) OUTPUT,
@Seed int = null
as
begin
declare @ch char,
@prevchar char,
@iLoop int,
@MINCHAR char,
@MAXCHAR char,
@passwordchar int,
@iUserNameLength int
-- Default variable values
set @iLoop = 1
set @MINCHAR = '0'
set @MAXCHAR = 'z'
set @iUserNameLength = len(@username)
-- Seed is allocated based on date. If supplied as parameter it can
calculate what the password was for a given day.
-- E.g. Date is 17th July 2003 so Seed will be 2003717
if @Seed is null
begin
set @Seed = convert(int,convert(varchar,DATEPART(yyyy, GETDATE()))
+ convert(varchar,DATEPART(mm, GETDATE()))
+ convert(varchar,DATEPART(dd, GETDATE())))
end
-- if user name is less than the password limit we want to temporarily
increase the username to match the limit.
if @iUserNameLength < @iPasswordLimit
begin
while len(@username) < @iPasswordLimit
begin
set @username = @username + left(reverse(@username), @iPasswordLimit -
len(@username))
end
end
-- Otherwise we need to make sure we pick the beginning and end of the
username if it is too long
-- Avoids possibility of users with similar names getting same password.
else if @iUserNameLength > @iPasswordLimit
set @username = left(@username,@iPasswordLimit/2) +
right(@username,@iPasswordLimit - (@iPasswordLimit/2))
-- print @username
-- Loop through the username and generate password based on previous
character
while @iLoop <= @iPasswordLimit
begin
select @ch = substring(left(@username,@iPasswordLimit), @iLoop, 1)
-- Get password character based on previouse character and making sure it
is within the range of 122 and 65.
set @passwordchar = ((ascii(@ch) +
ascii(upper(isnull(@prevchar,@MINCHAR))) + @Seed) %
(ASCII(@MAXCHAR)-ASCII(@MINCHAR))) + ASCII(@MINCHAR)
-- Get rid of Junk characters if exist.
if @passwordchar > 90 and @passwordchar < 97
set @passwordchar = (@passwordchar + 7)
-- Get rid of Junk characters if exist.
if @passwordchar > 57 and @passwordchar < 65
set @passwordchar = (@passwordchar - 7)
select @password = isnull(@password,'') + char(@passwordchar)
select @iLoop = @iLoop + 1
-- Allocate a new PrevChar
select @prevchar = char(((@passwordchar + (ascii(@ch) *
@[EMAIL PROTECTED])[EMAIL PROTECTED]) %
(ASCII(@MAXCHAR)-ASCII(@MINCHAR))) + ASCII(@MINCHAR))
end
Print @password
return @@ERROR
end
go
--
** Archive: http://www.mail-archive.com/dev%40lists.cfdeveloper.co.uk/
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
For human help, e-mail: [EMAIL PROTECTED]