/* Copyright 2009 Palm, Inc.  All rights reserved. */

//
// Main crypto functions
//

// Decodes the device key, using the developer secret.  This will happen a maximum
// 	of once per instance of the app.
//
// The decoded device key is subsequently hashed by the URL of the Youtube page
//	we are requesting.  See: getDeviceData 

var Crypto = Class.create ({ 
	getDeviceAuth: function( device_key ) {
		var result = PalmSystem.decrypt( "yt", device_key );
	    result = Base64.decode(result);
	    return result;
	},

	// Hashes a decrypted device key using the path of a URL we are requesting.
	// This should be wrapped around requests when we want high-quality video
	getDeviceData : function(decrypted_key, path) {
		path = this.stripHostFromPath(path);
		return b64_hmac_sha1(decrypted_key, path);
	},

	// Strips the host and optional port from a URL
	// eg. http://www.youtube.com/feeds/api/uploads -->
	//		/feeds/api/uploads
	stripHostFromPath : function(s) {
		if (!s.startsWith('http')) {
			// already stripped
			return s;
		}
		
		var ret = s.split('');
		ret.splice(0, 8);					// remove http(s)://
		ret.splice(0, ret.indexOf('/'));	// remove til first /
		
		return ret.join('');
	}
	
});