hpka.js
//If buffer, take it as is. If string, assume it's base64-encoded
If buffer, take it as is. If string, assume it's base64-encoded
If buffer take it as is . If string assume it ' s base64 - encoded
function processSessionBlob(sessionBlob){
	var sessionBuf;
	if (Buffer.isBuffer(sessionBlob)) sessionBuf = sessionBlob;
	else if (typeof sessionBlob == 'string') sessionBuf = new Buffer(sessionBlob, 'base64');
	else throw new TypeError('invalid type for sessionBlob');
	var byteIndex = 0;
	var versionNumber = sessionBuf[byteIndex];
	byteIndex++;
	if (versionNumber != 0x01){
		throw new RangeError('PROTOCOL_VERSION');
	}
	var usernameLength = sessionBuf[byteIndex];
	byteIndex++;
	if (usernameLength > remainingBytes()){
		throw new Error('malformed session request');
	}
	var username = sessionBuf.toString('utf8', byteIndex, byteIndex + usernameLength);
	var usernameBuf = sessionBuf.slice(byteIndex, byteIndex + usernameLength);
	byteIndex += usernameLength;
	var timestampLeft, timestampRight;
	timestampLeft = sessionBuf.readUInt32BE(byteIndex);
	byteIndex += 4;
	timestampRight = sessionBuf.readUInt32BE(byteIndex);
	byteIndex += 4;
	var timestamp = joinUInt(timestampLeft, timestampRight);
	var currentTimestamp = Math.floor(Date.now() / 1000);
	if (currentTimestamp >= timestamp + 120) throw new RangeError('Request is too old');
	var sessionIdLength = sessionBuf[byteIndex];
	byteIndex++;
	if (sessionIdLength > remainingBytes()){
		throw new Error('malformed session request');
	}
	var sessionId = sessionBuf.toString('utf8', byteIndex, byteIndex + sessionIdLength);
	var sessionIdBuf = sessionBuf.slice(byteIndex, byteIndex + sessionIdLength);
	byteIndex += sessionIdLength;
	function remainingBytes(){
		return sessionBuf.length - byteIndex;
	}
	return {username: username, usernameBuffer: usernameBuf, timestamp: timestamp, sessionId: sessionId, sessionIdBuffer: sessionIdBuf};
}
function processSessionBlob ( sessionBlob ) { var sessionBuf ; if ( Buffer . isBuffer ( sessionBlob ) ) sessionBuf = sessionBlob ; else if ( typeof sessionBlob == 'string' ) sessionBuf = new Buffer ( sessionBlob , 'base64' ) ; else throw new TypeError ( 'invalid type for sessionBlob' ) ; var byteIndex = 0 ; var versionNumber = sessionBuf [ byteIndex ] ; byteIndex ++ ; if ( versionNumber != 0x01 ) { throw new RangeError ( 'PROTOCOL_VERSION' ) ; } var usernameLength = sessionBuf [ byteIndex ] ; byteIndex ++ ; if ( usernameLength > remainingBytes ( ) ) { throw new Error ( 'malformed session request' ) ; } var username = sessionBuf . toString ( 'utf8' , byteIndex , byteIndex + usernameLength ) ; var usernameBuf = sessionBuf . slice ( byteIndex , byteIndex + usernameLength ) ; byteIndex += usernameLength ; var timestampLeft , timestampRight ; timestampLeft = sessionBuf . readUInt32BE ( byteIndex ) ; byteIndex += 4 ; timestampRight = sessionBuf . readUInt32BE ( byteIndex ) ; byteIndex += 4 ; var timestamp = joinUInt ( timestampLeft , timestampRight ) ; var currentTimestamp = Math . floor ( Date . now ( ) / 1000 ) ; if ( currentTimestamp >= timestamp + 120 ) throw new RangeError ( 'Request is too old' ) ; var sessionIdLength = sessionBuf [ byteIndex ] ; byteIndex ++ ; if ( sessionIdLength > remainingBytes ( ) ) { throw new Error ( 'malformed session request' ) ; } var sessionId = sessionBuf . toString ( 'utf8' , byteIndex , byteIndex + sessionIdLength ) ; var sessionIdBuf = sessionBuf . slice ( byteIndex , byteIndex + sessionIdLength ) ; byteIndex += sessionIdLength ; function remainingBytes ( ) { return sessionBuf . length - byteIndex ; } return { username : username , usernameBuffer : usernameBuf , timestamp : timestamp , sessionId : sessionId , sessionIdBuffer : sessionIdBuf } ; }
If buffer, take it as is.
If buffer take it as is .
//Reading the version number //Reading username length //Reading username //Reading timestamp //Reading sessionId length //Reading sessionId