2014-10-19 21 views
9

Sto tentando di utilizzare CC_SHA256_DIGEST_LENGTH in una delle mie funzioni in Swift e genera un errore perché non riesce a trovare quel simbolo. Ho provato di tutto, importando CommonCrypto nel bridge-header e provando la soluzione .map. Nulla funziona.SHA256 in Swift - Importazione del problema di framework

Come posso utilizzare CC_SHA256_DIGEST_LENGTH in Swift? Tutte le soluzioni sembrano aver smesso di funzionare. Grazie!

+3

È sempre utile includere il codice problema e il messaggio di errore esatto e completo nella domanda. – zaph

+0

E in questo caso anche le versioni del framework sarebbero belle. –

risposta

20

Aggiungere la seguente riga al vostro intestazione colmare:
#import <CommonCrypto/CommonHMAC.h>

Swift esempio 2.x:

func doSha256(#dataIn:NSData) -> NSData { 
    var shaOut: NSMutableData! = NSMutableData(length: Int(CC_SHA256_DIGEST_LENGTH)); 
    CC_SHA256(dataIn.bytes, CC_LONG(dataIn.length), UnsafeMutablePointer<UInt8>(shaOut.mutableBytes)); 

    return shaOut; 
} 

Swift 3.0 esempio:

func hashSHA256(data:Data) -> Data? { 
    var hashData = Data(count: Int(CC_SHA256_DIGEST_LENGTH)) 
    _ = hashData.withUnsafeMutableBytes {digestBytes in 
     data.withUnsafeBytes {messageBytes in 
      CC_SHA256(messageBytes, CC_LONG(data.count), digestBytes) 
     } 
    } 
    return hashData 
} 

let clearData = "clearData".data(using:String.Encoding.utf8)! 
print("clearData: \(clearData.map { String(format: "%02hhx", $0) }.joined())") 

let hash = hashSHA256(data:clearData) 
print("hash: \(hash!.map { String(format: "%02hhx", $0) }.joined())") 

uscita:

clearData: 636c6561724461746130313233343536
hash: aabc766b6b357564e41f4f912d494bccbfa16924b574abbdba9e3e9da0c8920a

non ho alcun quadri aggiunti nelle fasi di destinazione costruire.
Sei sicuro che l'intestazione del bridging sia impostata correttamente? Ho aggiunto il mio aggiungendo un file .m e lasciato che il sistema aggiungesse automaticamente l'intestazione di bridging e aggiorni le impostazioni di destinazione.

metodo hash Generale spostato dalla sezione relativa alla documentazione sunsetted:

Questa funzione prende il nome di hash e dati da hash e restituisce un dato:

 
name: A name of a hash function as a String 
data: The Data to be hashed 
returns: the hashed result as Data 
func hash(name:String, data:Data) -> Data? { 
    let algos = ["MD2": (CC_MD2, CC_MD2_DIGEST_LENGTH), 
       "MD4": (CC_MD4, CC_MD4_DIGEST_LENGTH), 
       "MD5": (CC_MD5, CC_MD5_DIGEST_LENGTH), 
       "SHA1": (CC_SHA1, CC_SHA1_DIGEST_LENGTH), 
       "SHA224": (CC_SHA224, CC_SHA224_DIGEST_LENGTH), 
       "SHA256": (CC_SHA256, CC_SHA256_DIGEST_LENGTH), 
       "SHA384": (CC_SHA384, CC_SHA384_DIGEST_LENGTH), 
       "SHA512": (CC_SHA512, CC_SHA512_DIGEST_LENGTH)] 
    guard let (hashAlgorithm, length) = algos[name] else { return nil } 
    var hashData = Data(count: Int(length)) 

    _ = hashData.withUnsafeMutableBytes {digestBytes in 
     data.withUnsafeBytes {messageBytes in 
      hashAlgorithm(messageBytes, CC_LONG(data.count), digestBytes) 
     } 
    } 
    return hashData 
} 

Nota: MD2, MD4, MD5 e SHA1 non devono essere utilizzati in un nuovo lavoro, non sono più sicuri per l'utilizzo del digest dei messaggi.

+0

Riceve ancora l'errore "Uso dell'identificatore non risolto 'CC_SHA256_DIGEST_LENGTH'" e gli stessi errori per CC_LONG e CC_SHA256. Ho l'importazione CommonCrypto nell'intestazione del ponte. Devo aggiungere qualche framework al progetto? ( – Hjalmar

+0

FINALMENTE! Il problema era che uno degli obiettivi non era spuntato quando ho importato il file .m per generare un'intestazione di bridging. Grazie per il tuo aiuto! – Hjalmar

+0

lo abbiamo trovato: http: // www.learnswiftonline.com/getting-started/adding-swift-bridging-header/ oh, anche importante https://stackoverflow.com/questions/26096402/xcode-myprojectname-bridging-header-h-does-not-exist – pete

Problemi correlati