Код: Выделить всё
var randomCodeStr: String
let passphrase = "secretsalt"
var userToken: String//a token identifying user
func makeCodeAndEmail {
let code = Int.random(in: 100000...999999)//make random code
randomCodeStr = String(code)//stringify
self.postToServer(randomCodestr,usertoken)//send to server to email to user
}
< /code>
Кодовый сервер PHP < /p>
$passphrase = "secretsalt";
$code = $_POST['randomcode'];//received from phone
function createCode($passphrase,$code)
{
$saltedCode = hash_hmac('sha1', $code, $passphrase); // salt code with phrase
$encodedCode = substr($saltedCode, 0, 6); // take first 6 characters of hash
return $encodedCode;
}
// обратно на телефон
swift
Код: Выделить всё
func verifyCode(_ code: String, passphrase: String) -> Bool {
let hashedCode = hashHMAC(code, key: passphrase)
let encodedCode = String(hashedCode.prefix(6))
return code == encodedCode
}
func hashHMAC(_ input: String, key: String) -> String {
let algo = CCHmacAlgorithm(kCCHmacAlgSHA1)
let str = input.cString(using: .utf8)
let keyStr = key.cString(using: .utf8)
var digest = [UInt8](repeating: 0, count: Int(CC_SHA1_DIGEST_LENGTH))
if let mystr = str, let myKeyStr = keyStr {
CCHmac(algo, myKeyStr, strlen(myKeyStr), str, strlen(mystr), &digest)
}
return Data(bytes: digest, count: digest.count).base64EncodedString()
}
>
Подробнее здесь: https://stackoverflow.com/questions/796 ... php-and-sw
Мобильная версия