Код: Выделить всё
// Create code from passphrase..In practice passphrase should be user-dependent such as name+id
$passphrase = "testing";
function createCode($passphrase)
{
$code = rand(100000, 999999); // generate 6-digit code
$saltedCode = hash_hmac('sha1', $code, $passphrase); // salt code with phrase
$encodedCode = substr($saltedCode, 0, 6); // take first 6 characters of hash
return $encodedCode;
}
// SEND THIS TO USER WHO THEN HAS TO ENTER INTO APP
// In app
< /code>
Swift Decode < /p>
let passphrase = "testing"
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
Мобильная версия