2019-11-27 17:20:43 +01:00
|
|
|
// Generated by Secrets.swift.gyb
|
2019-11-26 14:58:16 +01:00
|
|
|
%{
|
|
|
|
import os
|
|
|
|
|
2019-11-27 17:20:43 +01:00
|
|
|
secrets = ['FEED_WRANGLER_KEY']
|
|
|
|
|
2019-11-26 14:58:16 +01:00
|
|
|
def chunks(seq, size):
|
2019-11-27 17:20:43 +01:00
|
|
|
return (seq[i:(i + size)] for i in range(0, len(seq), size))
|
2019-11-26 14:58:16 +01:00
|
|
|
|
|
|
|
def encode(string, salt):
|
2019-11-27 17:20:43 +01:00
|
|
|
bytes = string.encode("UTF-8")
|
|
|
|
return [ord(bytes[i]) ^ salt[i % len(salt)] for i in range(0, len(bytes))]
|
|
|
|
|
|
|
|
def snake_to_camel(snake_str):
|
|
|
|
components = snake_str.split('_')
|
|
|
|
return components[0].lower() + ''.join(x.title() for x in components[1:])
|
2019-11-26 14:58:16 +01:00
|
|
|
|
|
|
|
salt = [ord(byte) for byte in os.urandom(64)]
|
|
|
|
}%
|
|
|
|
enum Secrets {
|
|
|
|
|
2019-11-27 17:20:43 +01:00
|
|
|
% for secret in secrets:
|
|
|
|
static var ${snake_to_camel(secret)}: String {
|
|
|
|
let encoded: [UInt8] = [
|
|
|
|
% for chunk in chunks(encode(os.environ.get(secret), salt), 8):
|
2019-11-26 14:58:16 +01:00
|
|
|
${"".join(["0x%02x, " % byte for byte in chunk])}
|
|
|
|
% end
|
|
|
|
]
|
|
|
|
|
|
|
|
return decode(encoded, salt: salt)
|
|
|
|
}
|
2019-11-27 17:20:43 +01:00
|
|
|
% end
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
private extension Secrets {
|
2019-11-26 14:58:16 +01:00
|
|
|
|
|
|
|
private static let salt: [UInt8] = [
|
|
|
|
% for chunk in chunks(salt, 8):
|
|
|
|
${"".join(["0x%02x, " % byte for byte in chunk])}
|
|
|
|
% end
|
|
|
|
]
|
|
|
|
|
|
|
|
private static func decode(_ encoded: [UInt8], salt: [UInt8]) -> String {
|
|
|
|
String(decoding: encoded.enumerated().map { (offset, element) in
|
|
|
|
element ^ salt[offset % salt.count]
|
|
|
|
}, as: UTF8.self)
|
|
|
|
}
|
2019-11-27 17:20:43 +01:00
|
|
|
|
2019-11-26 14:58:16 +01:00
|
|
|
}
|