UNPKG

1.6 kBPlain TextView Raw
1@:expose
2class Cyph {
3 private static var addressSpace = [
4 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
5 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
6 'k', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u',
7 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E',
8 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
9 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'
10 ].map(function (s: String) {
11 return s.charCodeAt(0);
12 });
13
14 private static var services = {
15 chat: 'cyph.im',
16 voice: 'cyph.audio',
17 video: 'cyph.video'
18 };
19
20 public static var options = {
21 voice: 1,
22 video: 2,
23 modestBranding: 3,
24 disableP2P: 4,
25 nativeCrypto: 5
26 };
27
28 private static function generateGuid (length: Int) : String {
29 var randomBytes = SecureRandom.getSecureRandomBytes(length);
30
31 for (i in 0...length) {
32 randomBytes.set(i, Cyph.addressSpace[Std.int(Math.floor(
33 randomBytes.get(i) / 256.0 * Cyph.addressSpace.length
34 ))]);
35 }
36
37 var guid = randomBytes.toString();
38 randomBytes.fill(0, length, 0);
39 return guid;
40 }
41
42 public static function initiateSession (?options: Array<Int>) : String {
43 if (options == null) {
44 options = [];
45 }
46
47 return 'https://' +
48 (
49 options.indexOf(Cyph.options.video) > -1 ?
50 Cyph.services.video :
51 options.indexOf(Cyph.options.voice) > -1 ?
52 Cyph.services.voice :
53 Cyph.services.chat
54 ) +
55 '/#' +
56 (options.indexOf(Cyph.options.modestBranding) > -1 ? '&' : '') +
57 (options.indexOf(Cyph.options.disableP2P) > -1 ? '$' : '') +
58 (options.indexOf(Cyph.options.nativeCrypto) > -1 ? '%' : '') +
59 Cyph.generateGuid(26)
60 ;
61 }
62}
63
\No newline at end of file