xml口令文件描述
adduser.asp文件用来向xml文件中增加新的元素。作为一个羽翼丰满的应用程序,还需要建立编辑和删除功能,但是如果需要的话,这些能够手工完成,而增加新用户则只能用这个页面来完成。这是因为口令和salt都是在用户提供的口令的基础上用aspencrypt生成的。adduser.asp中的大部分代码用来 操作xml文件并创建一个新元素,但是因为有其它文章专门解释如何用asp处理xml(见server side xml in asp),所以我在这里只涉及aspencrypt代码:
'create hash of password chosen
randomize
salt = ""
for i = 1 to 10
salt = salt & chr(int(rnd * 26) + 65) '65 is ascii for "a"
next
' calculate hash of password + salt
set cm = server.createobject("persits.cryptomanager")
set context = cm.opencontext("mycontainer", true)
set hash = context.createhash
hash.addtext request("password") & salt
hashvalue = hash.value.hex
set hash = nothing
set cm = nothing
首先,我们用randomize生成一个任意salt ,用vbscript rnd函数创建一个任意的10字符字符串。 然后,salt 把增加到提交给页面的口令中,并发送给cryptohash 对象。然后,单程随机值被提取 为hashvalue,然后将其存储在xml文件中。
validateuser.asp 文档具有同样的功能。为了确定用户名/口令组合是否有效,页面首先要看在xml文件中是否存在用户名。如果没有,它就返回用户没有找到。否则,它就要将提交口令加提交用户salt 的随机值与为用户存储的加密口令相比较。以下代码执行这个测试:
found = false
i = 0
while i < objectrootelement.childnodes.length and not found
if objectrootelement.childnodes(i).attributes _
.getnameditem("name").nodevalue = username then
found = true
else
i = i + 1
end if
wend
if found then
response.write "user " & username & " found"
' calculate hash of specified password + salt from db
hashvalue = objrootelement.childnodes(i).attributes _
.getnameditem("password").nodevalue
salt = objectrootelement.childnodes(i).attributes _
.getnameditem("salt").nodevalue
set cm = server.createobject("persits.cryptomanager")
set context = cm.opencontext("mycontainer", true)
set hash = context.createhash hash.addtext password & salt
hashvalue2 = hash.value.hex
set hash = nothing
set cm = nothing
if hashvalue = hashvalue2 then
response.write "user " & username & " validated"
else
response.write "user password invalid."
end if
else
response.write "user " & username & " not found"
end if
hashvalue用来在xml文件中存储经过加密的口令的值(正确的经过加密的口令)。hashvalue2 是用提交的口令和数据库中的salt 进行计算的。如果它们匹配,用户就被确认。否则就提交一个无效口令。