Encode simple passwords
file: passwd_lookup.rb (generates an xml file containing an alphanumeric index with corresponding cryptic values)
1 2 class PasswordLookup 3 4 def initialize() 5 chars = (0..9).to_a + Array.new(7) + ('A'..'Z').to_a + Array.new(6) + ('a'..'z').to_a 6 @chars = (0..9).to_a + ('A'..'Z').to_a + ('a'..'z').to_a 7 @doc = Document.new() 8 root = Element.new('codes') 9 @doc.add_element(root) 10 11 chars.each do |char| 12 node = Element.new('code') 13 if not char.nil? 14 node.attributes['index'] = char 15 node.attributes['value'] = get_random_chars(2) 16 end 17 root.add_element(node) 18 end 19 puts root 20 end 21 22 23 def save(filepath) 24 file = File.new(filepath,'w') 25 file.puts @doc 26 file.close 27 end 28 29 def get_random_chars(vword_size) 30 newpass = Array.new(rand(vword_size) + 1, '').collect{@chars[rand(@chars.size)]}.join 31 # return the encryption providing it doesn't already exist in the lookup table. 32 if not /value=\'#{newpass}\'/.match @doc.root.elements.to_a.to_s 33 return newpass 34 else 35 return get_random_chars(vword_size) 36 end 37 38 end 39 40 private :get_random_chars 41 42 end
output extract: (codes - see also http://rorbuilder.info/pl/codes)
1 2 <codes> 3 <code value='4h' index='a'/><code value='B' index='b'/><code value='m' index='c'/> 4 <code value='qf' index='d'/> 5 </codes>
file: password_lookup.js
1 2 var t; 3 var m_doc; 4 5 function loadXml() { 6 url = 'http://rorbuilder.info/pl/codes'; 7 m_doc = XML.load(url); 8 } 9 10 function getCode(val,i) { 11 pos = val.charCodeAt(i) - 48; 12 node = m_doc.documentElement.childNodes[pos] 13 return node.getAttribute('value'); 14 } 15 16 function timed_update(keyCode, val) { 17 if (val.length > 0 && ((keyCode > 40) || (keyCode == 8)) ) { 18 clearTimeout(t); 19 t = setTimeout("revealCode('" + val + "')", 1000); 20 } 21 else 22 { 23 o = document.getElementById('out1'); 24 if (val.length <= 0 && o.value.length > 0) { 25 o.value = ''; 26 } 27 } 28 29 } 30 31 function revealCode(val) { 32 var iEnd = val.length; 33 var newcode = ''; 34 for (i=0;i<iEnd;i++) { 35 36 var codex = getCode(val,i); 37 newcode += codex; 38 } 39 update(newcode); 40 } 41 42 function update(val){ 43 o = document.getElementById('out1'); 44 o.value = val; 45 /*var o_copied = document.getElementById('out1').createTextRange(); 46 o_copied.exeCommand("Copy");*/ 47 }
file: password_lookup.html
1 2 <body onload="loadXml()"> 3 <h1>Password lookup</h1> 4 <dl> 5 <dt><label for="in1">Enter password:</label></dt> 6 <dd><input type="text" name="in1" id="in1" value="" 7 onkeyup="timed_update(event.keyCode, this.value)" /></dd> 8 9 <dt><label for="out1">Generated password</label></dt> 10 <dd><input type="text" name="out1" id="out1" value=""/></dd> 11 <dd><input type="button" name="clear1" id="clear1" onclick="clearPassword()" value="clear"/></dd> 12 13 </dl> 14 <p>see also: <a href="codes.xml" title="password code lookup table">codes.xml</a></p> 15 </body>
Try out the encode a simple password demo [rorbuilder.info].
see also: Reading an XML file usng JavaScript [snippets.dzone.com]