-
Notifications
You must be signed in to change notification settings - Fork 2
/
Secrets.swift.gyb
47 lines (40 loc) · 1.34 KB
/
Secrets.swift.gyb
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
37
38
39
40
41
42
43
44
45
46
47
%{
import os
import json
import string
def to_camel_case(s):
return s[0].lower() + string.capwords(s, sep='_').replace('_', '')[1:] if s else s
def chunks(seq, size):
return (seq[i:(i + size)] for i in range(0, len(seq), size))
def encode(string, cipher):
bytes = string.encode("UTF-8")
return [ord(bytes[i]) ^ cipher[i % len(cipher)] for i in range(0, len(bytes))]
}%
enum Secrets {
private static let salt: [UInt8] = [
%{ salt = [ord(byte) for byte in os.urandom(64)] }%
% for chunk in chunks(salt, 8):
${"".join(["0x%02x, " % byte for byte in chunk])}
% end
]
static func decode(_ encoded: [UInt8], cipher: [UInt8]) -> String {
String(decoding: encoded.enumerated().map { (offset, element) in
element ^ cipher[offset % cipher.count]
}, as: UTF8.self)
}
% with open('secrets.json') as json_file:
% for key, value in json.load(json_file).items():
% if type(value) is unicode:
static var ${to_camel_case(key)}: String {
let encoded: [UInt8] = [
% for chunk in chunks(encode(value, salt), 8):
${"".join(["0x%02x, " % byte for byte in chunk])}
% end
]
return decode(encoded, cipher: salt)
}
% else:
% print("Non-string values are not supported: ", key, value)
% end
% end
}