Welcome to mirror list, hosted at ThFree Co, Russian Federation.

Hash.ts « util « src - github.com/jsxc/jsxc.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: e77aee726a5bcd773062bc006eac38a36edec573 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import * as sha1 from 'js-sha1';

export default class Hash {
   public static String(value: string) {
      let hash = 0;

      if (value.length === 0) {
         return hash;
      }

      for (let i = 0; i < value.length; i++) {
         hash = (hash << 5) - hash + value.charCodeAt(i);
         hash |= 0; // Convert to 32bit integer
      }

      return hash;
   }

   public static SHA1FromBase64(data: string): string {
      let base64 = data.replace(/^.+;base64,/, '');
      let buffer = base64ToArrayBuffer(base64);

      return sha1(buffer);
   }
}

function base64ToArrayBuffer(base64String: string) {
   let binaryString = window.atob(base64String);
   let bytes = new Uint8Array(binaryString.length);

   for (let i = 0; i < binaryString.length; i++) {
      bytes[i] = binaryString.charCodeAt(i);
   }

   return bytes.buffer;
}