NetNewsWire/Shared/Secrets.swift.gyb

52 lines
1.4 KiB
Plaintext
Raw Normal View History

// Generated by Secrets.swift.gyb
2019-11-26 14:58:16 +01:00
%{
import os
2024-02-25 03:56:44 +01:00
secrets = ['MERCURY_CLIENT_ID', 'MERCURY_CLIENT_SECRET', 'FEEDLY_CLIENT_ID', 'FEEDLY_CLIENT_SECRET', 'INOREADER_APP_ID', 'INOREADER_APP_KEY']
2019-11-26 14:58:16 +01:00
def chunks(seq, size):
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):
bytes_ = string.encode("UTF-8")
return [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 = [byte for byte in os.urandom(64)]
2019-11-26 14:58:16 +01:00
}%
import Secrets
public final class Secrets: SecretsProvider {
% for secret in secrets:
public lazy var ${snake_to_camel(secret)}: String = {
let encoded: [UInt8] = [
% for chunk in chunks(encode(os.environ.get(secret) or "", salt), 8):
2019-11-26 14:58:16 +01:00
${"".join(["0x%02x, " % byte for byte in chunk])}
% end
]
return decode(encoded, salt: salt)
}()
% end
%{
# custom example: static let myVariable = "${os.environ.get('MY_CUSTOM_VARIABLE')}"
}%
2019-11-26 14:58:16 +01:00
private let salt: [UInt8] = [
2019-11-26 14:58:16 +01:00
% for chunk in chunks(salt, 8):
${"".join(["0x%02x, " % byte for byte in chunk])}
% end
]
private func decode(_ encoded: [UInt8], salt: [UInt8]) -> String {
2019-11-26 14:58:16 +01:00
String(decoding: encoded.enumerated().map { (offset, element) in
element ^ salt[offset % salt.count]
}, as: UTF8.self)
}
}