summaryrefslogtreecommitdiff
path: root/WebInterface/src/js/modules/networking/hash.js
diff options
context:
space:
mode:
Diffstat (limited to 'WebInterface/src/js/modules/networking/hash.js')
-rw-r--r--WebInterface/src/js/modules/networking/hash.js20
1 files changed, 20 insertions, 0 deletions
diff --git a/WebInterface/src/js/modules/networking/hash.js b/WebInterface/src/js/modules/networking/hash.js
new file mode 100644
index 0000000..3abcc21
--- /dev/null
+++ b/WebInterface/src/js/modules/networking/hash.js
@@ -0,0 +1,20 @@
+/**
+ * Creates Base64 String with SHA-256 Hash of given string
+ */
+String.prototype.getHash = async function() {
+ let data = new ArrayBuffer(this.length * 2);
+ let bufferView = new Uint16Array(data);
+ for (let i = 0; i < this.length; i++) {
+ bufferView[i] = this.charCodeAt(i);
+ }
+
+ let encrypted = await crypto.subtle.digest('SHA-256', bufferView);
+ let byteArray = new Uint8Array(encrypted);
+ let base64String = '';
+
+ for (let byte of byteArray) {
+ base64String += String.fromCharCode(byte);
+ }
+
+ return btoa(base64String);
+};