NEWS
Fritz!Box Daten auslesen, Gerätetraffic und Filter setzen
-
Hallo zusammen,
um den Kindern ihren Medienkonsum etwas besser vor die Augen zu halten und ggf. nach Überschreiten von Zeitlimits das Internet zu sperren, habe ich aufbauend auf einem Script aus dem Netz dieses erweitert.
Im Prinzip spricht es das Fritzbox GUI an und holt- Die Geräteinformationen
- Listet die Profile auf
- Speichert Traffic von Geräten mit aktivem Trafficmonitoring in der InfluxDB-History auf
- Berechnet und speichert die Nutzungsdauer der Geräte in Minuten (Nutzung über Threshold b/s)
- Kann Filtern Geräten zuordnen, bspw. bei überschreiten eines Nutzungslimits von Gerätegruppen
Der Code ist nicht superschön und man sieht vermutlich, dass ich auf etwas anderem aufgesetzt habe, aber die Funktionen sind schon ganz nützlich.
Achso: setStateNotExist ist eine Funktion von mir. Müsste man mit setState ersetzen.
const Version = "2.0"; const InfluxClient = require('@influxdata/influxdb-client'); const limits = [{name: 'Ben', limit: 2*60, devices: ['Switch01','PS5_Ben','Fernseher_Ben','Ben_Laptop']}, {name: 'Max', limit: 2*60, devices: ['FireTVStick','Max Handy','Switch_2']}, ] // TODO // * Liste erzeugen mit Devices und aktuellem Filter // > als JSON in einen DP speichern -> für Tabellenauswertung // https://forum.iobroker.net/topic/16184/http-request-in-javascript/5 var request = require('request'); var headers = { 'Content-Type': 'application/x-www-form-urlencoded', 'User-Agent': 'curl/7.64.0', 'Accept': '*/*' }; /******************************************************* * E I N S T E L L U N G E N *******************************************************/ // Die IP Der Fritzbox const FbIp = "192.168.178.1"; // Der User der Fritzbox // HINWEIS : Den Benutzer leer lassen wenn die Anmeldung an der FB nur mit Passwort erfolgt ! const FbUser = "**************"; //Anmeldebenutzername für WebGUI, default unter den Users heraussuchen // Das Password der Fritzbox const FbPassword = "***************"; //Anmeldepasswort für WebGUI const FbDebugging = false; // Keine Änderung an der Fritzbox (true) -> Listet dann nur alle Profile und Rechner const FbListOnly = true; // Device / Filter Liste erzeugen Ja (true), Nein (false) var secChallenge; var secMd5; var secLogin; var secSid; var secProfileNames = []; var secProfileIds = []; var secDeviceNames = []; var secDeviceIds = []; var secDeviceData = []; var JsonList = ""; /******************************************************* * Object Trigger Version * Wenn auf einen Datenpunkt getriggert werden soll, dann muss der folgende Block auskommentiert werden. * FbIobObject muss mit einem string Datenpunkt versehen werden. * Den Static Teil dann auskommentieren ! * Diese Variante erlaubt es das Script dynamisch für alle Devices / Profile der FB zu verwenden. *******************************************************/ getFbChallenge(); var s1 = schedule('4,9,14,19,24,29,34,39,44,49,54,59 * * * *', function(){ secProfileNames = []; secProfileIds = []; secDeviceNames = []; secDeviceIds = []; secDeviceData = []; getFbChallenge(); }) /******************************************************* * Static Version * Die folgenden 3 Zeilen lassen das Script sofort laufen. * Dabei sollte der Object Trigger Block auskommentiert werden. *******************************************************/ // console.log("Profile Changer started ... (Version : " + Version + ")"); // getFbChallenge(); // console.log("Profile Changer done"); // Get the Challenge String from the FritzBox // Compute the md5 Hash function getFbChallenge(){ console.log("function getFbChallenge"); request.get({ url: 'http://' + FbIp + '/login_sid.lua?username=' + FbUser, headers: headers }, function(error, response, body) { if (error) log(error, 'error'); //console.log(response); secChallenge = body.match(/Challenge>(.*)<\/Challenge/)[1]; console.log(" > Challenge : " + secChallenge); var uft16le = str2rstr_utf16le(secChallenge + "-" +FbPassword ); var md5 = rstr_md5(uft16le); secMd5 = rstr2hex(md5); console.log(" > MD5 : " + secMd5); secLogin = "response=" + secChallenge + "-" + secMd5 + "&username=" + FbUser; console.log(" > Login : " + secLogin); getFbSid(); }); } // Get the SID from the Fritzbox function getFbSid() { console.log("function getFbSid"); request.post({ url: 'http://' + FbIp, headers: headers, form: secLogin }, function(error, response, body) { if (error) log(error, 'error'); // SID filtern // "sid":"c503b24dae458086" try { secSid = response.body.match(/\"sid\":\"(.*)\"/)[1]; } catch (e) { if (secSid == undefined) { console.log("Your login was not successful. End Script", Error) return; } } console.log(" > SID : " + secSid); //getFbDeviceInfos(); getFbProfiles(); }); } // TBD: // Netzwerkinfos mit allen Devices lesen: // var req = "xhr=1&sid=" + secSid + "&lang=de&page=netDev&xhrId=cleanup&useajax=1&no_sidrenew=" function getFbDeviceInfos(){ console.log("function getFbDeviceInfos"); var req = "xhr=1&sid=" + secSid + "&lang=de&page=netDev&xhrId=cleanup&useajax=1&no_sidrenew=" request.post({ url: 'http://' + FbIp + '/data.lua', headers: headers, form: req }, function(error, response, body) { if (error) log(error, 'error'); //console.log(response.body); var data = JSON.parse(response.body) for(var i = 0;i < data['data']['active'].length;i++){ var dataset = {"active":true} setStateNotExist('javascript.0.FritzBox.Devices.'+ data['data']['active'][i]['name']+'.active',true,true,true) for (let [key, value] of Object.entries( data['data']['active'][i])) { dataset[key] = value if(typeof value === "object"){ value = JSON.stringify(value) } setStateNotExist('javascript.0.FritzBox.Devices.'+ data['data']['active'][i]['name']+'.'+key,value,true,true) } secDeviceData.push(dataset) } for(var i = 0;i < data['data']['passive'].length;i++){ var dataset = {"active":true} setStateNotExist('javascript.0.FritzBox.Devices.'+ data['data']['passive'][i]['name']+'.active',false,true,true) for (let [key, value] of Object.entries( data['data']['passive'][i])) { dataset[key] = value if(typeof value === "object"){ value = JSON.stringify(value) } setStateNotExist('javascript.0.FritzBox.Devices.'+ data['data']['passive'][i]['name']+'.'+key,value,true,true) } secDeviceData.push(dataset) } //log(secDeviceData) getFbDeviceInfos2(); }); } function TrafficAboveThreshold(devName,value){ var threshold = 300*1024 for(var i = 0;i < thresholds.length;i++){ if(thresholds[i].name == devName){ threshold = thresholds[i].threshold } } if(value >= threshold){ return true }else{ return false } } async function getFbMointorDatasets(){ const influx_connection = new InfluxClient.InfluxDB({url:'http://192.168.178.46:8086',token:'_8XmsZizKYP35JNXhAI3pWYgOqWxMu425r1fw_93oQO6q0XebI1UBsdCGcLW6k1T52JnS7ewYDvobKYoz8sdow==',timeout:120000}) const influx_write = await influx_connection.getWriteApi('pi4', 'network','ms') console.log("function getFbMointorDatasets"); var custom_header = { 'Content-Type': 'application/json', 'User-Agent': 'curl/7.64.0', 'Accept': '*/*', 'Authorization': 'AVM-SID '+secSid, 'Referer': 'http://' + FbIp + '/' }; request.get({ url: 'http://' + FbIp + '/api/v0/monitor/datasets', headers: custom_header, }, async function(error, response, body) { if (error) log(error, 'error'); var data = JSON.parse(response.body) var urls = ['/api/v0/monitor/macaddrs/subset0000','/api/v0/monitor/onlinemonitor_dsl_0/subset0000','/api/v0/monitor/macaddrs/subset0002','/api/v0/monitor/onlinemonitor_dsl_0/subset0002'] for(var x = 0;x<urls.length;x++){ var subset = await getFbMointorDatasubsets(urls[x]) for(var i = 0; i < subset.length;i++){ for(var j = 0;j < data.length;j++){ for(var k = 0;k < data[j]['dataSources'].length;k++){ if(data[j]['dataSources'][k]['dataSourceName'] == subset[i]['dataSourceName']){ var bfound = false if(data[j]['dataSources'][k]['landeviceUid'] != ''){ for(var z = 0;z < secDeviceData.length;z++){ if(secDeviceData[z]['UID'] == data[j]['dataSources'][k]['landeviceUid']){ //log(subset[i]['dataSourceName'] + ' -> ' + data[j]['dataSources'][k]['landeviceUid'] + ' -> ' + secDeviceData[z]['name'] + ' ' + data[j]['dataSources'][k]['type']) bfound = true data[j]['dataSources'][k]['name'] = secDeviceData[z]['name'] } } } if(bfound){ data[j]['dataSources'][k]['subset000'+x] = subset[i] if(urls[x].search('subset0000') > 0){ var d = Date.parse(subset[i]['timestamp']) for(var t = subset[i]['measurements'].length-1; t >= 0;t--){ var ts = d - (subset[i]['measurements'].length-1-t) * 5*1000 if(subset[i]['measurements'][t] != null && data[j]['dataSources'][k]['name'] != undefined && data[j]['dataSources'][k]['name'] != 'undefinded'){ //if(data[j]['dataSources'][k]['name'] == "FireTVStick" && data[j]['dataSources'][k]['type'] == "downstream")log(new Date(ts).toLocaleString()+ ' ' +subset[i]['measurements'][t]) var point1 = new InfluxClient.Point(data[j]['dataSources'][k]['name']) .floatField(data[j]['dataSources'][k]['type'], subset[i]['measurements'][t]) .timestamp(ts) influx_write.writePoint(point1) } } } } } } } } } await influx_write.flush() await influx_write.close() await update_utilization(); }); } async function getFbMointorDatasubsets(subsetURL){ console.log("function getFbMointorDatasets "+subsetURL); var custom_header = { 'Content-Type': 'application/json', 'User-Agent': 'curl/7.64.0', 'Accept': '*/*', 'Authorization': 'AVM-SID '+secSid, 'Referer': 'http://' + FbIp + '/' }; return new Promise(resolve => { request.get({ url: 'http://' + FbIp + subsetURL, headers: custom_header, }, function(error, response, body) { if (error){log(error, 'error');resolve([])} //console.log(response.body); resolve(JSON.parse(response.body)) }); }) } // Get all Profiles from the Fritzbox function getFbProfiles() { console.log("function getFbProfiles"); // #curl -d "xhr=1&sid=${sid}&lang=de&no_sidrenew=&page=kidPro" "http://$1/data.lua" var req = "xhr=1&sid=" + secSid + "&lang=de&no_sidrenew=&page=kidPro"; request.post({ url: 'http://' + FbIp + '/data.lua', headers: headers, form: req }, function(error, response, body) { if (error) log(error, 'error'); if (FbDebugging){ console.log(" > response.body : \n" + response.body); } console.log(" > Decode Names") var rx = new RegExp( /class=\"name\"\stitle=\"([a-zA-Z0-9 äöüÄÖÜ\-\_\.]*)\"\sdatalabel/g ); while( (match = rx.exec( body )) != null ) { secProfileNames.push(match[1]); } if (FbDebugging){ console.log(" > secProfileNames : \n" + secProfileNames); } console.log(" > Decode Filters") // submit" name="edit" value=" rx = new RegExp( /submit\"\sname=\"edit\"\svalue=\"([a-zA-Z0-9 äöüÄÖÜ\-\_\.]*)\"\sclass=\"icon/g ); while( (match = rx.exec( body )) != null ) { secProfileIds.push(match[1]); } if (FbDebugging){ console.log(" > secProfileIds : \n" + secProfileIds); } if (FbListOnly) { console.log("Filter Count : " + secProfileIds.length); for (var i = 0; i < secProfileIds.length; i++) { //console.log("Filter named '"+ secProfileNames[i] + "' has ID : " + secProfileIds[i]); setStateNotExist('javascript.0.FritzBox.Profiles.' + secProfileNames[i] + '.UID',secProfileIds[i],true,true) } } getFbProfilesInfo(); }); } async function getFbProfilesInfo() { console.log("function getFbProfilesInfo"); // xhr=1&sid=5f5ba302815594d8&lang=de&no_sidrenew=&page=kidLis for (var i = 0; i < secProfileIds.length; i++) { log('listing Profile ' + secProfileNames[i] + ' ' + secProfileIds[i]) var req = "xhr=1&back_to_page=kidPro&sid=" + secSid + "&lang=de&no_sidrenew=&page=kids_profileedit&edit=" + secProfileIds[i]; await request.post({ url: 'http://' + FbIp + '/data.lua', headers: headers, form: req }, function(error, response, body) { if (error) log(error, 'error'); if (FbDebugging){ console.log(" > response.body : \n" + response.body); } //log(response.body) let rx let match var profileName = "undefinded" rx = new RegExp( /<input\stype=\"text\".*id=\"uiName\"\svalue=\"(?<name>.*)\"\s/g ); if( (match = rx.exec( body )) != null ) { var { name } = match.groups profileName = name } //log(profileName) //Decode rx = new RegExp( /<input\s+type="radio"\s(?<checked>checked\s)?id="uiTime:(?<id>\w+)"/g ); while( (match = rx.exec( body )) != null ) { const { id, checked } = match.groups setStateNotExist('javascript.0.FritzBox.Profiles.' + profileName + '.uiTime_'+ id,checked == undefined ? false : true,true,true) } rx = new RegExp( /<input\s+type="radio"\s(?<checked>checked\s)?id="uiBudget:(?<id>\w+)"/g ); while( (match = rx.exec( body )) != null ) { const { id, checked } = match.groups setStateNotExist('javascript.0.FritzBox.Profiles.' + profileName + '.uiBudget_'+ id,checked == undefined ? false : true,true,true) } body = body.slice(body.search('Zugeordnete Netzwerkgeräte')) rx = /<table[\s\S]*?<div id="js-add-more-network-devices-container"/; if (rx.test(body)) { //already linked devices available var linked = [] rx = new RegExp( /<td>(?<link>[a-zA-Z0-9 äöüÄÖÜ\-\_\.]*)<\/td>/g ); while( (match = rx.exec( body )) != null ) { const { link } = match.groups linked.push(link) setStateNotExist('javascript.0.FritzBox.Devices.' + link + '.profile',profileName,true,true) } //log(linked) setStateNotExist('javascript.0.FritzBox.Profiles.' + profileName + '.linked',JSON.stringify(linked),true,true) } }); } getFbDevices(); } // IMPORTANT: // The DeviceId changes when you switch the profile. // If you use default profiles it is something like "landevice308962" // If you use your own profiles it is something like "user7749" function getFbDevices() { console.log("function getFbDevices"); // xhr=1&sid=5f5ba302815594d8&lang=de&no_sidrenew=&page=kidLis var req = "xhr=1&sid=" + secSid + "&lang=de&no_sidrenew=&page=kidLis"; request.post({ url: 'http://' + FbIp + '/data.lua', headers: headers, form: req }, function(error, response, body) { if (error) log(error, 'error'); if (FbDebugging){ console.log(" > response.body : \n" + response.body); } // TESTING //body = getState("Global.0.Testing.StringValue").val; secDeviceNames = []; secDeviceIds = []; console.log(" > Decode Device Names") var rx = new RegExp( /class=\"name"\stitle=\"([a-zA-Z0-9 äöüÄÖÜ\-\_\.]*)\"\sdatalabel/g ); while( (match = rx.exec( body )) != null ) { secDeviceNames.push(match[1]); } if (FbDebugging){ console.log(" > secDeviceNames : \n" + secDeviceNames); } console.log(" > Decode Device Ids") rx = new RegExp( /name=\"profile:([a-zA-Z0-9 äöüÄÖÜ\-\_\.]*)\"><option/g ); while( (match = rx.exec( body )) != null ) { secDeviceIds.push(match[1]); } if (FbDebugging){ console.log(" > secDeviceIds : \n" + secDeviceIds); } console.log("Device Count : " + secDeviceIds.length); for (var i = 0; i < secDeviceNames.length; i++) { setStateNotExist('javascript.0.FritzBox.Devices.'+secDeviceNames[i]+'.UID',secDeviceIds[i],true,true) if(!existsState('javascript.0.FritzBox.Devices.'+secDeviceNames[i]+'.profile'))setStateNotExist('javascript.0.FritzBox.Devices.'+secDeviceNames[i]+'.profile','Standard',true,true) //console.log(i + " - Device named '"+ secDeviceNames[i] + "' has ID : " + secDeviceIds[i]); } console.log("DONE : Listmode - No Device blocking ..."); getFbDeviceInfos(); }); } function getFbDeviceInfos2() { console.log("function getFbDeviceInfos2"); var custom_header = { 'Accept': '*/*', 'Authorization': 'AVM-SID '+secSid, 'Origin': 'http://' + FbIp, 'Referer': 'http://' + FbIp + '/', 'Content-Type': 'application/json', 'User-Agent': 'curl/7.64.0', }; request.get({ url: 'http://' + FbIp + '/api/v0/landevice', headers: custom_header }, function(error, response, body) { if (error) log(error, 'error'); if (FbDebugging){ console.log(" > response : \n" + response); } var data = JSON.parse(response.body) //log('Devices2: '+ data['landevice'].length) var fields = [] for(var i = 0;i < data['landevice'].length;i++){ for(const key in data['landevice'][i]){ if(fields.indexOf(key) == -1)fields[key] = false } } for(var i = 0;i < data['landevice'].length;i++){ var fields_tmp = fields.slice() for(const key in data['landevice'][i]){ fields_tmp[key] = true setStateNotExist('javascript.0.FritzBox.Devices.'+ data['landevice'][i]['friendly_name'] +'.' + key, data['landevice'][i][key],true,true) } for(const key in fields_tmp){ if(fields_tmp[key] == false)setStateNotExist('javascript.0.FritzBox.Devices.'+ data['landevice'][i]['friendly_name'] +'.' + key, "",true,true) } } getFbMointorDatasets(); }); } function setFbSperre(devName,profileName) { console.log("function setFbSperre"); var devID = getState('javascript.0.FritzBox.Devices.' + devName + '.UID').val var profileID = getState('javascript.0.FritzBox.Profiles.'+profileName+'.UID').val var userID = getState('javascript.0.FritzBox.Devices.' + devName + '.user_UIDs').val var current_profile = getState('javascript.0.FritzBox.Devices.' + devName + '.profile').val if(profileID == null || devID == null){ log('error resolving FBSperre Vars:'+profileID+' '+profileName+' '+devID+' '+devName) } var custom_header = { 'Accept': '*/*', 'Authorization': 'AVM-SID '+secSid, 'Origin': 'http://' + FbIp, 'Referer': 'http://' + FbIp + '/', 'Content-Type': 'application/json', 'User-Agent': 'curl/7.64.0', }; //log(data) var method = 'POST' var url = 'http://' + FbIp + '/api/v0/user/user' var data = {"type":"1","landeviceUID":devID,"filter_profile_UID":profileID} if(userID != '' && current_profile != 'Standard'){ method = 'PUT' url = 'http://' + FbIp + '/api/v0/user/user/'+userID data = {"filter_profile_UID":profileID,"disallowed":"0"} } request({ method: method, url: url, headers: custom_header, body: JSON.stringify(data) }, function(error, response, body) { if (error) log(error, 'error'); if (FbDebugging){ console.log(" > response : \n" + response); } //log(response.body.length) if(response.body.length < 10){ setStateNotExist('javascript.0.FritzBox.Devices.'+ devName +'.profile',profileName,true,true) }else{ log(response.body) log(devName + ' -> ' + profileName) log(userID) log(data) } }); } function check_limits(){ for(var i = 0;i < limits.length;i++){ log('Checking limits for '+limits[i].name) var active_sum = 0 for(var d = 0;d < limits[i].devices.length;d++){ var value = getState('javascript.0.FritzBox.Monitor.'+ limits[i].devices[d] +'.activeduration_in_min').val if(value !== null && value !== undefined){ active_sum += value } } log(limits[i].name+ ' used up '+ active_sum +'min of his '+limits[i].limit +'min limit') var sProfile = 'Begrenztes Youtube_Tiktok' if(limits[i].limit <= active_sum){ log('locking devices:') sProfile = 'Kein Internet' }else{ log('unlocking devices (if needed):') sProfile = 'Begrenztes Youtube_Tiktok' } for(var d = 0;d < limits[i].devices.length;d++){ //log(limits[i].devices[d]) var value = getState('javascript.0.FritzBox.Devices.'+ limits[i].devices[d] +'.profile').val if(value !== null && value !== undefined){ if(value != sProfile){ log('Switching profile for '+limits[i].devices[d]+': ' +value+' -> '+sProfile) setFbSperre(limits[i].devices[d],sProfile) } } } } } async function update_utilization(start = new Date().setHours(0,0,0,0)){ var query = `data = from(bucket: "network") |> range(start: `+start/1000+`, stop: 0m) |> filter(fn: (r) => r._field == "downstream" or r._field == "upstream") |> pivot( rowKey: ["_time", "_measurement"], columnKey: ["_field"], valueColumn: "_value" ) |> map(fn: (r) => ({ _time: r._time, _measurement: r._measurement, _value: r.downstream + r.upstream })) sumAgg = data |> aggregateWindow( every: 1m, fn: sum, offset: 1m, createEmpty: false ) |> set(key: "_field", value: "total_sum") maxAgg = data |> aggregateWindow( every: 1m, fn: max, offset: 1m, createEmpty: false ) |> set(key: "_field", value: "total_max") union(tables: [sumAgg, maxAgg]) |> pivot( rowKey: ["_time", "_measurement"], columnKey: ["_field"], valueColumn: "_value" ) |> rename(columns: { total_sum: "total_sum", total_max: "total_max" }) |> keep(columns: ["_time", "_measurement", "total_sum", "total_max"])` try{ var result = await new Promise(resolve => { sendTo('influxdb.0', 'query', query , {timeout: 120000}, function (res) { resolve(res) })}) var t = result['result'][0] var data = {} for(var i = t.length-1; i>=1;i--){ data[t[i]['_measurement']] = {} data[t[i]['_measurement']]['active_count'] = 0 data[t[i]['_measurement']]['inactive_count'] = 0 } for(var i = t.length-1; i>=1;i--){ if(t[i]['total_max'] != null){ if(TrafficAboveThreshold(t[i]['_measurement'],t[i]['total_max'])){ //log(data[j]['dataSources'][k]['name']+' '+data[j]['dataSources'][k]['type']+' '+new Date(ts).toLocaleString()+': '+subset[i]['measurements'][t]) data[t[i]['_measurement']]['active_count'] += 1 if(data[t[i]['_measurement']]['inactive_count'] > 0 && data[t[i]['_measurement']]['inactive_count'] <= 10){ log('expanded staggered usage '+ t[i]['_measurement'] + ': '+ data[t[i]['_measurement']]['active_count'] + 'active '+data[t[i]['_measurement']]['inactive_count']+'inactive') data[t[i]['_measurement']]['active_count'] += data[t[i]['_measurement']]['inactive_count'] } data[t[i]['_measurement']]['inactive_count'] = 0 }else{ data[t[i]['_measurement']]['inactive_count'] += 1 if(data[t[i]['_measurement']]['active_count'] > 0 && data[t[i]['_measurement']]['inactive_count'] >= 15 && data[t[i]['_measurement']]['active_count'] <= 10){ log('skipped small usage '+ t[i]['_measurement'] + ': '+ data[t[i]['_measurement']]['active_count'] + 'active '+data[t[i]['_measurement']]['inactive_count']+'inactive') data[t[i]['_measurement']]['active_count'] = 0 } } } } log(data) for(const key in data){ setStateNotExist('javascript.0.FritzBox.Monitor.'+ key +'.activeduration_in_min',data[key]['active_count'],true,true) setStateNotExist('javascript.0.FritzBox.Monitor.'+ key +'.inactiveduration_in_min',data[key]['inactive_count'],true,true) setStateNotExist('javascript.0.FritzBox.Monitor.'+ key +'.activeduration_in_percent',data[key]['active_count']/2/60*100,true,true) setStateNotExist('javascript.0.FritzBox.Monitor.'+ key +'.inactiveduration_in_percent',data[key]['inactive_count']/2/20*100,true,true) } check_limits(); }catch(e){ log('error fetching data: '+e) } return null } // https://gist.githubusercontent.com/josedaniel/951664/raw/33c7a6b44d6cc53dc75cb5230fb551bc4ba7a46a/md5.js // http://pajhome.org.uk/crypt/md5/instructions.html /* * A JavaScript implementation of the RSA Data Security, Inc. MD5 Message * Digest Algorithm, as defined in RFC 1321. * Version 2.2 Copyright (C) Paul Johnston 1999 - 2009 * Other contributors: Greg Holt, Andrew Kepert, Ydnar, Lostinet * Distributed under the BSD License * See http://pajhome.org.uk/crypt/md5 for more info. */ /* * Configurable variables. You may need to tweak these to be compatible with * the server-side, but the defaults work in most cases. */ var hexcase = 0; /* hex output format. 0 - lowercase; 1 - uppercase */ var b64pad = ""; /* base-64 pad character. "=" for strict RFC compliance */ /* * These are the functions you'll usually want to call * They take string arguments and return either hex or base-64 encoded strings */ function hex_md5(s) { return rstr2hex(rstr_md5(str2rstr_utf8(s))); } function b64_md5(s) { return rstr2b64(rstr_md5(str2rstr_utf8(s))); } function any_md5(s, e) { return rstr2any(rstr_md5(str2rstr_utf8(s)), e); } function hex_hmac_md5(k, d) { return rstr2hex(rstr_hmac_md5(str2rstr_utf8(k), str2rstr_utf8(d))); } function b64_hmac_md5(k, d) { return rstr2b64(rstr_hmac_md5(str2rstr_utf8(k), str2rstr_utf8(d))); } function any_hmac_md5(k, d, e) { return rstr2any(rstr_hmac_md5(str2rstr_utf8(k), str2rstr_utf8(d)), e); } /* * Perform a simple self-test to see if the VM is working */ function md5_vm_test() { return hex_md5("abc").toLowerCase() == "900150983cd24fb0d6963f7d28e17f72"; } /* * Calculate the MD5 of a raw string */ function rstr_md5(s) { return binl2rstr(binl_md5(rstr2binl(s), s.length * 8)); } /* * Calculate the HMAC-MD5, of a key and some data (raw strings) */ function rstr_hmac_md5(key, data) { var bkey = rstr2binl(key); if(bkey.length > 16) bkey = binl_md5(bkey, key.length * 8); var ipad = Array(16), opad = Array(16); for(var i = 0; i < 16; i++) { ipad[i] = bkey[i] ^ 0x36363636; opad[i] = bkey[i] ^ 0x5C5C5C5C; } var hash = binl_md5(ipad.concat(rstr2binl(data)), 512 + data.length * 8); return binl2rstr(binl_md5(opad.concat(hash), 512 + 128)); } /* * Convert a raw string to a hex string */ function rstr2hex(input) { try { hexcase } catch(e) { hexcase=0; } var hex_tab = hexcase ? "0123456789ABCDEF" : "0123456789abcdef"; var output = ""; var x; for(var i = 0; i < input.length; i++) { x = input.charCodeAt(i); output += hex_tab.charAt((x >>> 4) & 0x0F) + hex_tab.charAt( x & 0x0F); } return output; } /* * Convert a raw string to a base-64 string */ function rstr2b64(input) { try { b64pad } catch(e) { b64pad=''; } var tab = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; var output = ""; var len = input.length; for(var i = 0; i < len; i += 3) { var triplet = (input.charCodeAt(i) << 16) | (i + 1 < len ? input.charCodeAt(i+1) << 8 : 0) | (i + 2 < len ? input.charCodeAt(i+2) : 0); for(var j = 0; j < 4; j++) { if(i * 8 + j * 6 > input.length * 8) output += b64pad; else output += tab.charAt((triplet >>> 6*(3-j)) & 0x3F); } } return output; } /* * Convert a raw string to an arbitrary string encoding */ function rstr2any(input, encoding) { var divisor = encoding.length; var i, j, q, x, quotient; /* Convert to an array of 16-bit big-endian values, forming the dividend */ var dividend = Array(Math.ceil(input.length / 2)); for(i = 0; i < dividend.length; i++) { dividend[i] = (input.charCodeAt(i * 2) << 8) | input.charCodeAt(i * 2 + 1); } /* * Repeatedly perform a long division. The binary array forms the dividend, * the length of the encoding is the divisor. Once computed, the quotient * forms the dividend for the next step. All remainders are stored for later * use. */ var full_length = Math.ceil(input.length * 8 / (Math.log(encoding.length) / Math.log(2))); var remainders = Array(full_length); for(j = 0; j < full_length; j++) { quotient = Array(); x = 0; for(i = 0; i < dividend.length; i++) { x = (x << 16) + dividend[i]; q = Math.floor(x / divisor); x -= q * divisor; if(quotient.length > 0 || q > 0) quotient[quotient.length] = q; } remainders[j] = x; dividend = quotient; } /* Convert the remainders to the output string */ var output = ""; for(i = remainders.length - 1; i >= 0; i--) output += encoding.charAt(remainders[i]); return output; } /* * Encode a string as utf-8. * For efficiency, this assumes the input is valid utf-16. */ function str2rstr_utf8(input) { var output = ""; var i = -1; var x, y; while(++i < input.length) { /* Decode utf-16 surrogate pairs */ x = input.charCodeAt(i); y = i + 1 < input.length ? input.charCodeAt(i + 1) : 0; if(0xD800 <= x && x <= 0xDBFF && 0xDC00 <= y && y <= 0xDFFF) { x = 0x10000 + ((x & 0x03FF) << 10) + (y & 0x03FF); i++; } /* Encode output as utf-8 */ if(x <= 0x7F) output += String.fromCharCode(x); else if(x <= 0x7FF) output += String.fromCharCode(0xC0 | ((x >>> 6 ) & 0x1F), 0x80 | ( x & 0x3F)); else if(x <= 0xFFFF) output += String.fromCharCode(0xE0 | ((x >>> 12) & 0x0F), 0x80 | ((x >>> 6 ) & 0x3F), 0x80 | ( x & 0x3F)); else if(x <= 0x1FFFFF) output += String.fromCharCode(0xF0 | ((x >>> 18) & 0x07), 0x80 | ((x >>> 12) & 0x3F), 0x80 | ((x >>> 6 ) & 0x3F), 0x80 | ( x & 0x3F)); } return output; } /* * Encode a string as utf-16 */ function str2rstr_utf16le(input) { var output = ""; for(var i = 0; i < input.length; i++) output += String.fromCharCode( input.charCodeAt(i) & 0xFF, (input.charCodeAt(i) >>> 8) & 0xFF); return output; } function str2rstr_utf16be(input) { var output = ""; for(var i = 0; i < input.length; i++) output += String.fromCharCode((input.charCodeAt(i) >>> 8) & 0xFF, input.charCodeAt(i) & 0xFF); return output; } /* * Convert a raw string to an array of little-endian words * Characters >255 have their high-byte silently ignored. */ function rstr2binl(input) { var output = Array(input.length >> 2); for(var i = 0; i < output.length; i++) output[i] = 0; for(var i = 0; i < input.length * 8; i += 8) output[i>>5] |= (input.charCodeAt(i / 8) & 0xFF) << (i%32); return output; } /* * Convert an array of little-endian words to a string */ function binl2rstr(input) { var output = ""; for(var i = 0; i < input.length * 32; i += 8) output += String.fromCharCode((input[i>>5] >>> (i % 32)) & 0xFF); return output; } /* * Calculate the MD5 of an array of little-endian words, and a bit length. */ function binl_md5(x, len) { /* append padding */ x[len >> 5] |= 0x80 << ((len) % 32); x[(((len + 64) >>> 9) << 4) + 14] = len; var a = 1732584193; var b = -271733879; var c = -1732584194; var d = 271733878; for(var i = 0; i < x.length; i += 16) { var olda = a; var oldb = b; var oldc = c; var oldd = d; a = md5_ff(a, b, c, d, x[i+ 0], 7 , -680876936); d = md5_ff(d, a, b, c, x[i+ 1], 12, -389564586); c = md5_ff(c, d, a, b, x[i+ 2], 17, 606105819); b = md5_ff(b, c, d, a, x[i+ 3], 22, -1044525330); a = md5_ff(a, b, c, d, x[i+ 4], 7 , -176418897); d = md5_ff(d, a, b, c, x[i+ 5], 12, 1200080426); c = md5_ff(c, d, a, b, x[i+ 6], 17, -1473231341); b = md5_ff(b, c, d, a, x[i+ 7], 22, -45705983); a = md5_ff(a, b, c, d, x[i+ 8], 7 , 1770035416); d = md5_ff(d, a, b, c, x[i+ 9], 12, -1958414417); c = md5_ff(c, d, a, b, x[i+10], 17, -42063); b = md5_ff(b, c, d, a, x[i+11], 22, -1990404162); a = md5_ff(a, b, c, d, x[i+12], 7 , 1804603682); d = md5_ff(d, a, b, c, x[i+13], 12, -40341101); c = md5_ff(c, d, a, b, x[i+14], 17, -1502002290); b = md5_ff(b, c, d, a, x[i+15], 22, 1236535329); a = md5_gg(a, b, c, d, x[i+ 1], 5 , -165796510); d = md5_gg(d, a, b, c, x[i+ 6], 9 , -1069501632); c = md5_gg(c, d, a, b, x[i+11], 14, 643717713); b = md5_gg(b, c, d, a, x[i+ 0], 20, -373897302); a = md5_gg(a, b, c, d, x[i+ 5], 5 , -701558691); d = md5_gg(d, a, b, c, x[i+10], 9 , 38016083); c = md5_gg(c, d, a, b, x[i+15], 14, -660478335); b = md5_gg(b, c, d, a, x[i+ 4], 20, -405537848); a = md5_gg(a, b, c, d, x[i+ 9], 5 , 568446438); d = md5_gg(d, a, b, c, x[i+14], 9 , -1019803690); c = md5_gg(c, d, a, b, x[i+ 3], 14, -187363961); b = md5_gg(b, c, d, a, x[i+ 8], 20, 1163531501); a = md5_gg(a, b, c, d, x[i+13], 5 , -1444681467); d = md5_gg(d, a, b, c, x[i+ 2], 9 , -51403784); c = md5_gg(c, d, a, b, x[i+ 7], 14, 1735328473); b = md5_gg(b, c, d, a, x[i+12], 20, -1926607734); a = md5_hh(a, b, c, d, x[i+ 5], 4 , -378558); d = md5_hh(d, a, b, c, x[i+ 8], 11, -2022574463); c = md5_hh(c, d, a, b, x[i+11], 16, 1839030562); b = md5_hh(b, c, d, a, x[i+14], 23, -35309556); a = md5_hh(a, b, c, d, x[i+ 1], 4 , -1530992060); d = md5_hh(d, a, b, c, x[i+ 4], 11, 1272893353); c = md5_hh(c, d, a, b, x[i+ 7], 16, -155497632); b = md5_hh(b, c, d, a, x[i+10], 23, -1094730640); a = md5_hh(a, b, c, d, x[i+13], 4 , 681279174); d = md5_hh(d, a, b, c, x[i+ 0], 11, -358537222); c = md5_hh(c, d, a, b, x[i+ 3], 16, -722521979); b = md5_hh(b, c, d, a, x[i+ 6], 23, 76029189); a = md5_hh(a, b, c, d, x[i+ 9], 4 , -640364487); d = md5_hh(d, a, b, c, x[i+12], 11, -421815835); c = md5_hh(c, d, a, b, x[i+15], 16, 530742520); b = md5_hh(b, c, d, a, x[i+ 2], 23, -995338651); a = md5_ii(a, b, c, d, x[i+ 0], 6 , -198630844); d = md5_ii(d, a, b, c, x[i+ 7], 10, 1126891415); c = md5_ii(c, d, a, b, x[i+14], 15, -1416354905); b = md5_ii(b, c, d, a, x[i+ 5], 21, -57434055); a = md5_ii(a, b, c, d, x[i+12], 6 , 1700485571); d = md5_ii(d, a, b, c, x[i+ 3], 10, -1894986606); c = md5_ii(c, d, a, b, x[i+10], 15, -1051523); b = md5_ii(b, c, d, a, x[i+ 1], 21, -2054922799); a = md5_ii(a, b, c, d, x[i+ 8], 6 , 1873313359); d = md5_ii(d, a, b, c, x[i+15], 10, -30611744); c = md5_ii(c, d, a, b, x[i+ 6], 15, -1560198380); b = md5_ii(b, c, d, a, x[i+13], 21, 1309151649); a = md5_ii(a, b, c, d, x[i+ 4], 6 , -145523070); d = md5_ii(d, a, b, c, x[i+11], 10, -1120210379); c = md5_ii(c, d, a, b, x[i+ 2], 15, 718787259); b = md5_ii(b, c, d, a, x[i+ 9], 21, -343485551); a = safe_add(a, olda); b = safe_add(b, oldb); c = safe_add(c, oldc); d = safe_add(d, oldd); } return Array(a, b, c, d); } /* * These functions implement the four basic operations the algorithm uses. */ function md5_cmn(q, a, b, x, s, t) { return safe_add(bit_rol(safe_add(safe_add(a, q), safe_add(x, t)), s),b); } function md5_ff(a, b, c, d, x, s, t) { return md5_cmn((b & c) | ((~b) & d), a, b, x, s, t); } function md5_gg(a, b, c, d, x, s, t) { return md5_cmn((b & d) | (c & (~d)), a, b, x, s, t); } function md5_hh(a, b, c, d, x, s, t) { return md5_cmn(b ^ c ^ d, a, b, x, s, t); } function md5_ii(a, b, c, d, x, s, t) { return md5_cmn(c ^ (b | (~d)), a, b, x, s, t); } /* * Add integers, wrapping at 2^32. This uses 16-bit operations internally * to work around bugs in some JS interpreters. */ function safe_add(x, y) { var lsw = (x & 0xFFFF) + (y & 0xFFFF); var msw = (x >> 16) + (y >> 16) + (lsw >> 16); return (msw << 16) | (lsw & 0xFFFF); } /* * Bitwise rotate a 32-bit number to the left. */ function bit_rol(num, cnt) { return (num << cnt) | (num >>> (32 - cnt)); } onStop (function scriptStop () { clearSchedule(s1); }, 1000);