Skip to content

Commit

Permalink
Readd calculation of an application hash value
Browse files Browse the repository at this point in the history
  • Loading branch information
ahzf committed Apr 16, 2024
1 parent e05d1c4 commit 282cb41
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 43 deletions.
71 changes: 30 additions & 41 deletions src/ts/chargyApp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ export class ChargyApp {

this.appEdition = appEdition ?? "";
this.copyright = copyright ?? "";
this.versionsURL = versionsURL ?? "https://open.charging.cloud/desktop/versions";
this.versionsURL = versionsURL ?? "https://chargy.charging.cloud/apps/web/versions";
this.showFeedbackSection = showFeedbackSection ?? false;
this.defaultFeedbackEMail = feedbackEMail ?? [];
this.defaultFeedbackHotline = feedbackHotline ?? [];
Expand All @@ -132,7 +132,7 @@ export class ChargyApp {

//#endregion

//#region Load external data from file system
//#region Load JavaScript libraries

this.elliptic = require('elliptic');
this.moment = require('moment');
Expand Down Expand Up @@ -326,7 +326,6 @@ export class ChargyApp {
//#endregion



//#region Handle the 'Update available'-button

this.updateAvailableButton.onclick = (ev: MouseEvent) => {
Expand Down Expand Up @@ -600,6 +599,13 @@ export class ChargyApp {
accessToken: 'pk.eyJ1IjoiYWh6ZiIsImEiOiJOdEQtTkcwIn0.Cn0iGqUYyA6KPS8iVjN68w'
}).addTo(this.map);


//#region Calculate application hash

this.calcApplicationHash();

//#endregion

}


Expand Down Expand Up @@ -677,6 +683,8 @@ export class ChargyApp {





//#region UpdateFeedbackSection()

public UpdateFeedbackSection(FeedbackEMail?: String[],
Expand Down Expand Up @@ -936,49 +944,30 @@ export class ChargyApp {

//#region calcApplicationHash(...)

private calcApplicationHash(filename1: string,
filename2: string,
onSuccess: (applicationHash: string) => void,
OnError: (errorMessage: string) => void)
private async calcApplicationHash()
{

const fs = require('original-fs');
const sha512a = require('crypto').createHash('sha512');
const stream1 = fs.createReadStream(filename1);
const applicationHashValueDiv = this.applicationHashValueDiv;

stream1.on('data', function(data: any) {
sha512a.update(data)
})

stream1.on('error', function() {
OnError("File '" + filename1 + "' not found!");
})
const files = [
'index.html',
'css/chargy.css',
'chargyWebApp-bundle.js',
'i18n.json',
'package.json'
];

stream1.on('end', function() {

const sha512b = require('crypto').createHash('sha512');
const stream2 = fs.createReadStream(filename2);

stream2.on('data', function(data: any) {
sha512b.update(data)
})

stream2.on('error', function() {
OnError("File '" + filename2 + "' not found!");
})

stream2.on('end', function() {

var sha512hash = require('crypto').createHash('sha512');
sha512hash.update(sha512a.digest('hex'));
sha512hash.update(sha512b.digest('hex'));
try
{

onSuccess(sha512hash.digest('hex'));
const hashes = await Promise.all(files.map(url => chargyLib.hashFile(url)));
const combinedHash = await crypto.subtle.digest('SHA-512', chargyLib.ConcatenateBuffers(hashes));

})
this.applicationHash = chargyLib.buf2hex(combinedHash);
this.applicationHashValueDiv.innerHTML = this.applicationHash.match(/.{1,8}/g)?.join(" ") ?? "";

})
} catch (error) {
console.error(`An error occurred: ${error}`);
return "";
}

}

Expand Down Expand Up @@ -2697,7 +2686,7 @@ export class ChargyApp {
const app = new ChargyApp(
"",
"© 2018-2024 GraphDefined GmbH",
"https://chargy.charging.cloud/desktop/versions", //"https://raw.githubusercontent.com/OpenChargingCloud/ChargyDesktopApp/master/versions/versions.json",
"https://chargy.charging.cloud/apps/web/versions",
true, // Show Feedback Section
["[email protected]", "?subject=Chargy%20WebApp%20Support"],
undefined, //["+4993219319101", "+49 9321 9319 101"],
Expand Down
43 changes: 41 additions & 2 deletions src/ts/chargyLib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
* limitations under the License.
*/

import * as exp from 'node:constants';
import * as chargyInterfaces from './chargyInterfaces'
import Decimal from 'decimal.js';

Expand Down Expand Up @@ -259,7 +258,10 @@ export function createHexString(arr: Iterable<number>) {
}

export function buf2hex(buffer: ArrayBuffer) {
return Array.prototype.map.call(new Uint8Array(buffer), (x:number) => ('00' + x.toString(16)).slice(-2)).join('');
//return Array.prototype.map.call(new Uint8Array(buffer), (x:number) => ('00' + x.toString(16)).slice(-2)).join('');
return Array.from(new Uint8Array(buffer))
.map(byte => byte.toString(16).padStart(2, '0'))
.join('');
}

export function hexToArrayBuffer(hex: string): ArrayBuffer {
Expand All @@ -278,6 +280,43 @@ export function hexToArrayBuffer(hex: string): ArrayBuffer {

}


export async function hashFile(url: string): Promise<ArrayBuffer>
{
try
{

const response = await fetch(url);

if (!response.ok)
throw new Error(`Failed to fetch ${url}: ${response.statusText}`);

return await crypto.subtle.digest('SHA-512', await response.arrayBuffer());

} catch (error) {
console.error(`Error fetching or hashing ${url}: ${error}`);
return new ArrayBuffer(0);
}
}

export function ConcatenateBuffers(buffers: ArrayBuffer[]): ArrayBuffer {

const totalLength = buffers.reduce((acc, value) => acc + value.byteLength, 0);
const result = new Uint8Array(totalLength);
let length = 0;

for (const buffer of buffers) {
result.set(new Uint8Array(buffer), length);
length += buffer.byteLength;
}

return result.buffer;

}




export function intFromBytes(x: number[]){

let val = 0;
Expand Down

0 comments on commit 282cb41

Please sign in to comment.