// digauth.js
// Copyright (C) 2001 Robert Cunnings
// routines for digest authentication

function BuildCN()
{
	var tm = (new Date()).getTime();
	var strHash = calcMD5(tm.toString(16));
	return strHash;
}


function BuildResponse(strSecret, strSN)
{
	// response = MD5( MD5(uid:realm:pwd):SN )

	var strTemp1 = calcMD5(strSecret);

	var strTemp2 = strTemp1;
	strTemp2 += ":";
	strTemp2 += strSN;

	strTemp1 = calcMD5(strTemp2);
	return strTemp1;
}

function BuildResponseMutual(strSecret, strSN, strCN)
{
	// response = MD5( MD5(uid:realm:pwd):SN:CN )

	var strTemp1 = calcMD5(strSecret);

	var strTemp2 = strTemp1;
	strTemp2 += ":";
	strTemp2 += strSN;
	strTemp2 += ":";
	strTemp2 += strCN;

	strTemp1 = calcMD5(strTemp2);
	return strTemp1;
}

function AuthServerMutual(strSecret, strSN, strCN, strRAuth)
{
     // response = MD5( MD5(uid:realm:pwd):SN:CN )
	var strTemp1 = calcMD5(strSecret);

	var strTemp2 = strTemp1;
	strTemp2 += ":";
	strTemp2 += strSN;
	strTemp2 += ":";
	strTemp2 += strCN;

	strTemp1 = calcMD5(strTemp2);
	
	if (strTemp1 != strRAuth)
	{
		return false;
	}
	return true;
}
