summaryrefslogtreecommitdiff
path: root/WebInterface/src/js/modules/networking/hash.js
blob: 3abcc2165693702b912ca3be2a6fd6db89772f74 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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);
};