{"id":16,"date":"2006-03-30T20:33:35","date_gmt":"2006-03-30T18:33:35","guid":{"rendered":"http:\/\/arjarapu.com\/wordpress\/?p=16"},"modified":"2010-02-27T20:55:51","modified_gmt":"2010-02-28T02:25:51","slug":"sql-server-generate-password-function","status":"publish","type":"post","link":"https:\/\/www.arjarapu.com\/wordpress\/2006\/03\/sql-server-generate-password-function\/","title":{"rendered":"SQL SERVER Generate Password Function"},"content":{"rendered":"<p>Most of you might have come across need for creating a random password for the users. I prefer using passwords that are generated completely random, containing a-zA-Z0-9 and some special characters, making them tough to guess. However, thinking from the user\u00e2\u20ac\u2122s perspective those passwords are really hard to remember. So, if you want to leverage complexity of the passwords for making them easy to remember then here is what I have used.<!--more--><\/p>\n<p>The key concept behind my algorithm is, a set of letters (like Alex) forming a meaningful name is easy to remember than a set of letters (like 6ZuK3s) that makes no sense. I compiled a table with Given Names pulled from <a href=\"http:\/\/www.sparkleware.com\/lists.html\">sparkleware<\/a>. To generate a random password, I pick a random given name and pull out only first 6 characters from it. Then I create a random set of special characters and set of random hex digits from NewID, UUID\/GUID.  The random given-name part, special chars part, hex digits part all together frame a random password. This is makes password not only easy to remember, compared to completely randomized text, but also somewhat tough to predict them as well.<\/p>\n<p>Please note that this technique might generate passwords that can be predicted using<a href=\"http:\/\/en.wikipedia.org\/wiki\/Dictionary_attack\"> dictionary attack<\/a>, if considerable precautions were not taken. If your application&#8217;s password needs to highly secure then, I suggest you follow completely randomized text generation as detailed in <a href=\"http:\/\/codebetter.com\/blogs\/raymond.lewallen\/archive\/2005\/05\/17\/63281.aspx\">here<\/a>, <\/p>\n<pre class='brush:sql'>\r\nALTER FUNCTION dbo.udf_generate_password(@newid VARCHAR(50), @rand REAL) RETURNS VARCHAR(100)\r\nAS\r\nBEGIN\r\n--****************************************************\r\n--   FUNCTION: DBO.UDF_GENERATE_PASSWOR\r\n--   AUTHOR: SHYAM K. ARJARAPU\r\n--   PARAMETER: @NEWID = NEWID() A UUID\/GUID\/NEWGUID\r\n--\t     @RAND = RAND() \r\n--   REASON: STUPID SQL SERVER DOES NOT ALLOW NON-\r\n--      DETERMINISTIC FUNCTIONS RAND(), GETDATE(), NEWID()  \r\n--      INSIDE THE FUNCTIONS.\r\n--  DESCRIPTION: THOUGH RANDOM PASSWORDS ARE SECURE, THEY\r\n--     ARE HARD TO REMEMBER. USERS CAN EASILY REMEMBER \r\n--     GROUP OF LETTERS FORMING A WORD. THE TABLE \r\n--     DBO.TB_PASSWORD_DICTIONARY CONTAINS SET OF FIRST NAMES\r\n--     FROM WHICH A RANDOM NAME IS SELECTED AND A SPECIAL\r\n--     CHARACTER WITH RANDOM STRING IS APPENDED TO MAKE THE\r\n--     PASSWORD NOT ONLY SECURE BUT ALSO ITS EASY TO REMEMBER \r\n--  USAGE: \r\n--     SELECT dbo.udf_generate_password(NEWID(), RAND())\r\n--*****************************************************\r\n\tDECLARE @randPassword VARCHAR(100)\r\n\tDECLARE @randNewID VARCHAR(10)\r\n\tDECLARE @randFirstName VARCHAR(6)\r\n\tDECLARE @randSpecialChar VARCHAR(2)\r\n\tDECLARE @maxID INT\r\n\tDECLARE @CONST_FN_LEN TINYINT\r\n\tDECLARE @CONST_NID_LEN TINYINT\r\n\tDECLARE @CONST_SPL_LEN TINYINT\r\n\tDECLARE @CONST_SPL_CHARS VARCHAR(15)\r\n\r\n\t--DEFINE THE REQUIRED LENGTHS &amp; SPECIAL CHARS\r\n\tSET @CONST_FN_LEN = 6\r\n\tSET @CONST_NID_LEN = 4\r\n\tSET @CONST_SPL_LEN = 2\r\n\tSET @CONST_SPL_CHARS = '@#$^*;,._!'\r\n\r\n\t--FRAME THE RANDOM TEXT PART\r\n\tSELECT @randNewID = LEFT(@newid, @CONST_NID_LEN)\r\n\r\n\tSELECT @maxID = MAX([KEY_ID]) \r\n\t  FROM dbo.tb_password_dictionary -- Contains KEY_ID Identity(1,1), [Value] Varchar. All the given-names goes here\r\n\r\n\t--FRAME RANDOM FIRST NAME PART\r\n\tSELECT @randFirstName = LEFT([VALUE], @CONST_FN_LEN)\r\n\t  FROM dbo.tb_password_dictionary \r\n\t WHERE [KEY_ID] = CONVERT(INT, @rand *@maxID) + 1\r\n\r\n\tSET @randSpecialChar = ''\r\n\r\n\t--FRAME THE SPECIAL CHARTS PART\r\n\tWHILE( @CONST_SPL_LEN &lt;&gt; 0)\r\n\tBEGIN\r\n\t\tSET @randSpecialChar = @randSpecialChar + \r\n\t\t\tSUBSTRING(@CONST_SPL_CHARS, CONVERT(INT, @rand * LEN(@CONST_SPL_CHARS)) + 1, 1) \r\n\t\tSET @rand = @rand * @rand\r\n\t\tSET @CONST_SPL_LEN = @CONST_SPL_LEN - 1\r\n\tEND \r\n\t\r\n\t--FRAME THE PASSWORD ALTOGETHER\r\n\tSET @randPassword = @randFirstName + @randSpecialChar + @randNewID\r\n\tRETURN @randPassword\r\nEND\r\n<\/pre>\n<p>&#8211; Shyam K. Arjarapu<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Most of you might have come across need for creating a random password for the users. I prefer using passwords that are generated completely random, containing a-zA-Z0-9 and some special characters, making them tough to guess. However, thinking from the &hellip; <a href=\"https:\/\/www.arjarapu.com\/wordpress\/2006\/03\/sql-server-generate-password-function\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[24,16,17],"_links":{"self":[{"href":"https:\/\/www.arjarapu.com\/wordpress\/wp-json\/wp\/v2\/posts\/16"}],"collection":[{"href":"https:\/\/www.arjarapu.com\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.arjarapu.com\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.arjarapu.com\/wordpress\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.arjarapu.com\/wordpress\/wp-json\/wp\/v2\/comments?post=16"}],"version-history":[{"count":4,"href":"https:\/\/www.arjarapu.com\/wordpress\/wp-json\/wp\/v2\/posts\/16\/revisions"}],"predecessor-version":[{"id":153,"href":"https:\/\/www.arjarapu.com\/wordpress\/wp-json\/wp\/v2\/posts\/16\/revisions\/153"}],"wp:attachment":[{"href":"https:\/\/www.arjarapu.com\/wordpress\/wp-json\/wp\/v2\/media?parent=16"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.arjarapu.com\/wordpress\/wp-json\/wp\/v2\/categories?post=16"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.arjarapu.com\/wordpress\/wp-json\/wp\/v2\/tags?post=16"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}