Skip to content

Instantly share code, notes, and snippets.

@chriswill0w
Forked from DejanEnspyra/Obfuscator.swift
Last active February 17, 2021 07:49
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chriswill0w/33fbcd767f9b58faeaaba6e1d1665b62 to your computer and use it in GitHub Desktop.
Save chriswill0w/33fbcd767f9b58faeaaba6e1d1665b62 to your computer and use it in GitHub Desktop.
Obfuscation of hard-coded security-sensitive strings.
protocol ObfuscatorProtocol {
#if DEBUG
func bytesByObfuscatingString(_ string: String) -> [UInt8]
#endif
func reveal(_ key: [UInt8]) -> String
}
class Obfuscator: ObfuscatorProtocol {
// MARK: - Variables
/// The salt used to obfuscate and reveal the string.
private var salt: String = ""
// MARK: - Initialization
init(withSalt salt: [AnyObject]) {
self.salt = salt.description
}
// MARK: - ObfuscatorProtocol Methods
#if DEBUG
/// This method obfuscates the string passed in using the salt
/// that was used when the Obfuscator was initialized.
///
/// - Parameter string: the string to obfuscate
/// - Returns: the obfuscated string in a byte array
func bytesByObfuscatingString(_ string: String) -> [UInt8] {
let text = [UInt8](string.utf8)
let cipher = [UInt8](salt.utf8)
let length = cipher.count
var encrypted = [UInt8]()
for t in text.enumerated() {
encrypted.append(t.element ^ cipher[t.offset % length])
}
#if DEBUG
print("Salt used: \(salt)\n")
print("Swift Code:\n************")
print("// Original \"\(string)\"")
print("let key: [UInt8] = \(encrypted)\n")
#endif
return encrypted
}
#endif
/// This method reveals the original string from the obfuscated
/// byte array passed in. The salt must be the same as the one
/// used to encrypt it in the first place.
///
/// - Parameter key: the byte array to reveal
/// - Returns: the original string
func reveal(_ key: [UInt8]) -> String {
let cipher = [UInt8](salt.utf8)
let length = cipher.count
var decrypted = [UInt8]()
for k in key.enumerated() {
decrypted.append(k.element ^ cipher[k.offset % length])
}
return String(bytes: decrypted, encoding: .utf8)!
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment