ios - swift 3 : Convert SHA256 hash string to SecCertificate
<p><p><code>Alamofire</code> 允许使用证书和公钥进行固定(尽管从包中获取公钥的函数从包中的证书中获取 key )。 </p>
<p>当从证书中提取公钥时,我能够使固定工作,<strong>但是当我提供 <code>SHA256</code> <code>String</code> 作为公钥</strong>(我从 api 调用接收 key 字符串,如果第一次固定失败,它应该用作公钥。)我使用下面的代码将字符串转换为 </p>
<p>//创建服务器信任策略</p>
<pre><code>let serverTrustPolicies: = [
destinationURL!: .pinPublicKeys(
publicKeys:savePublicKeys(),
validateCertificateChain:true,
validateHost:true
)]
self.manager = SessionManager(
serverTrustPolicyManager: ServerTrustPolicyManager(policies: serverTrustPolicies))
</code></pre>
<p>//获取</p>
<pre><code>func savePublicKeys() ->
{
var key:SecKey?
var publicKeys: = []
//Check and use if backup key is received from beacon call
if(KeychainService().checkIfKeyExists(tag: "backupURL"))
{
key = KeychainService().obtainKey(tag: backupURLKey)
publicKeys.append(key!)
}
return publicKeys
}
</code></pre>
<p>//插入和检索钥匙串(keychain)数据的函数</p>
<pre><code>func insertPublicKey(publicTag: String, data: Data) -> SecKey? {
let query: Dictionary<String, AnyObject> = [
String(kSecAttrKeyType): kSecAttrKeyClassPublic,
String(kSecClass): kSecClassKey as CFString,
String(kSecAttrApplicationTag): publicTag as CFString,
String(kSecValueData): data as CFData,
String(kSecReturnPersistentRef): true as CFBoolean]
var persistentRef: AnyObject?
let status = SecItemAdd(query as CFDictionary, &persistentRef)
if status != noErr && status != errSecDuplicateItem {
return nil
}
return obtainKey(tag: publicTag)
}
func obtainKey(tag: String) -> SecKey? {
var keyRef: AnyObject?
let query: Dictionary<String, AnyObject> = [
String(kSecAttrKeyType): kSecAttrKeyClassPublic,
String(kSecReturnRef): kCFBooleanTrue as CFBoolean,
String(kSecClass): kSecClassKey as CFString,
String(kSecAttrApplicationTag): tag as CFString,
String(kSecReturnPersistentRef): true as CFBoolean
]
let status = SecItemCopyMatching(query as CFDictionary, &keyRef)
switch status {
case noErr:
if let ref = keyRef {
return (ref as! SecKey)
}
default:
break
}
return nil
}
</code></pre>
<p>我哪里错了?据我所知,我使用的 <code>String</code> 是一个 <code>base64encoded</code> 并且适用于 Android 部分。</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>这适用于那些可能在试图找到相同或相似问题的答案时遇到困难的人。</p>
<p><strong>简短回答:</strong>散列是一种单行道。虽然<em>理论上</em>您也许可以尝试不同的输入来获取哈希值,从而获得问题中所需的证书数据,但<em>实际上</em>很难做到。已经编写了散列算法来防止您想要在这里实现的目标。要获得所需的输入,您可能需要花费大量的时间、空间和计算能力。</p>
<p><strong>长答案</strong>详细了解散列的真正作用。</p>
<p>例如,对于问题中的 SHA256,有 22562256 个可能的哈希值。如果您尝试了 22552255 个不同的输入,则有 50% 的机会。即使您每微秒尝试一次,这也将花费您 10631063 年。这就是为什么这实际上难以实现的主要原因之一。</p>
<p>反转哈希就像试图从它们的和中猜测两个数字 (x+y = 234)。有<em>很多</em>种可能的组合。
有一些很棒的答案<a href="https://crypto.stackexchange.com/questions/30481/why-cant-i-reverse-a-hash-to-a-possible-input?rq=1" rel="noreferrer noopener nofollow">here</a> </p></p>
<p style="font-size: 20px;">关于ios -swift3 : Convert SHA256 hash string to SecCertificate,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/41539477/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/41539477/
</a>
</p>
页:
[1]