diff --git a/Sodium/Box.swift b/Sodium/Box.swift index dd59a65..3f2f5ae 100644 --- a/Sodium/Box.swift +++ b/Sodium/Box.swift @@ -41,7 +41,7 @@ public class Box { guard let sk = NSMutableData(length: SecretKeyBytes) else { return nil } - if crypto_box_keypair(pk.mutableBytesPtr, sk.mutableBytesPtr) != 0 { + if crypto_box_keypair(pk.mutableBytesPtr(), sk.mutableBytesPtr()) != 0 { return nil } return KeyPair(publicKey: PublicKey(data: pk as Data), secretKey: SecretKey(data: sk as Data)) @@ -57,7 +57,7 @@ public class Box { guard let sk = NSMutableData(length: SecretKeyBytes) else { return nil } - if crypto_box_seed_keypair(pk.mutableBytesPtr, sk.mutableBytesPtr, seed.bytesPtr) != 0 { + if crypto_box_seed_keypair(pk.mutableBytesPtr(), sk.mutableBytesPtr(), seed.bytesPtr()) != 0 { return nil } return KeyPair(publicKey: PublicKey(data: pk as Data), secretKey: SecretKey(data: sk as Data)) @@ -67,7 +67,7 @@ public class Box { guard let nonce = NSMutableData(length: NonceBytes) else { return nil } - randombytes_buf(nonce.mutableBytesPtr, nonce.length) + randombytes_buf(nonce.mutableBytesPtr(), nonce.length) return nonce as Nonce } @@ -90,7 +90,7 @@ public class Box { guard let nonce = self.nonce() else { return nil } - if crypto_box_easy(authenticatedCipherText.mutableBytesPtr, message.bytesPtr, CUnsignedLongLong(message.length), nonce.bytesPtr, recipientPublicKey.bytesPtr, senderSecretKey.bytesPtr) != 0 { + if crypto_box_easy(authenticatedCipherText.mutableBytesPtr(), message.bytesPtr(), CUnsignedLongLong(message.length), nonce.bytesPtr(), recipientPublicKey.bytesPtr(), senderSecretKey.bytesPtr()) != 0 { return nil } return (authenticatedCipherText: authenticatedCipherText, nonce: nonce) @@ -109,7 +109,7 @@ public class Box { guard let nonce = self.nonce() else { return nil } - if crypto_box_detached(authenticatedCipherText.mutableBytesPtr, mac.mutableBytesPtr, message.bytesPtr, CUnsignedLongLong(message.length), nonce.bytesPtr, recipientPublicKey.bytesPtr, senderSecretKey.bytesPtr) != 0 { + if crypto_box_detached(authenticatedCipherText.mutableBytesPtr(), mac.mutableBytesPtr(), message.bytesPtr(), CUnsignedLongLong(message.length), nonce.bytesPtr(), recipientPublicKey.bytesPtr(), senderSecretKey.bytesPtr()) != 0 { return nil } return (authenticatedCipherText: authenticatedCipherText, nonce: nonce as Nonce, mac: mac as MAC) @@ -134,7 +134,7 @@ public class Box { guard let message = NSMutableData(length: authenticatedCipherText.length - MacBytes) else { return nil } - if crypto_box_open_easy(message.mutableBytesPtr, authenticatedCipherText.bytesPtr, CUnsignedLongLong(authenticatedCipherText.length), nonce.bytesPtr, senderPublicKey.bytesPtr, recipientSecretKey.bytesPtr) != 0 { + if crypto_box_open_easy(message.mutableBytesPtr(), authenticatedCipherText.bytesPtr(), CUnsignedLongLong(authenticatedCipherText.length), nonce.bytesPtr(), senderPublicKey.bytesPtr(), recipientSecretKey.bytesPtr()) != 0 { return nil } return message @@ -150,7 +150,7 @@ public class Box { guard let message = NSMutableData(length: authenticatedCipherText.length) else { return nil } - if crypto_box_open_detached(message.mutableBytesPtr, authenticatedCipherText.bytesPtr, mac.bytesPtr, CUnsignedLongLong(authenticatedCipherText.length), nonce.bytesPtr, senderPublicKey.bytesPtr, recipientSecretKey.bytesPtr) != 0 { + if crypto_box_open_detached(message.mutableBytesPtr(), authenticatedCipherText.bytesPtr(), mac.bytesPtr(), CUnsignedLongLong(authenticatedCipherText.length), nonce.bytesPtr(), senderPublicKey.bytesPtr(), recipientSecretKey.bytesPtr()) != 0 { return nil } return message @@ -158,7 +158,7 @@ public class Box { public func beforenm(recipientPublicKey: PublicKey, senderSecretKey: SecretKey) -> NSData? { let key = NSMutableData(length: BeforenmBytes) - if crypto_box_beforenm(key!.mutableBytesPtr, recipientPublicKey.bytesPtr, senderSecretKey.bytesPtr) != 0 { + if crypto_box_beforenm(key!.mutableBytesPtr(), recipientPublicKey.bytesPtr(), senderSecretKey.bytesPtr()) != 0 { return nil } return key @@ -174,7 +174,7 @@ public class Box { guard let nonce = self.nonce() else { return nil } - if crypto_box_easy_afternm(authenticatedCipherText.mutableBytesPtr, message.bytesPtr, CUnsignedLongLong(message.length), nonce.bytesPtr, beforenm.bytesPtr) != 0 { + if crypto_box_easy_afternm(authenticatedCipherText.mutableBytesPtr(), message.bytesPtr(), CUnsignedLongLong(message.length), nonce.bytesPtr(), beforenm.bytesPtr()) != 0 { return nil } return (authenticatedCipherText: authenticatedCipherText, nonce: nonce) @@ -199,7 +199,7 @@ public class Box { guard let message = NSMutableData(length: authenticatedCipherText.length - MacBytes) else { return nil } - if crypto_box_open_easy_afternm(message.mutableBytesPtr, authenticatedCipherText.bytesPtr, CUnsignedLongLong(authenticatedCipherText.length), nonce.bytesPtr, beforenm.bytesPtr) != 0 { + if crypto_box_open_easy_afternm(message.mutableBytesPtr(), authenticatedCipherText.bytesPtr(), CUnsignedLongLong(authenticatedCipherText.length), nonce.bytesPtr(), beforenm.bytesPtr()) != 0 { return nil } return message @@ -221,7 +221,7 @@ public class Box { guard let anonymousCipherText = NSMutableData(length: SealBytes + message.length) else { return nil } - if crypto_box_seal(anonymousCipherText.mutableBytesPtr, message.bytesPtr, CUnsignedLongLong(message.length), recipientPublicKey.bytesPtr) != 0 { + if crypto_box_seal(anonymousCipherText.mutableBytesPtr(), message.bytesPtr(), CUnsignedLongLong(message.length), recipientPublicKey.bytesPtr()) != 0 { return nil } return anonymousCipherText @@ -235,7 +235,7 @@ public class Box { if message == nil { return nil } - if crypto_box_seal_open(message!.mutableBytesPtr, anonymousCipherText.bytesPtr, CUnsignedLongLong(anonymousCipherText.length), recipientPublicKey.bytesPtr, recipientSecretKey.bytesPtr) != 0 { + if crypto_box_seal_open(message!.mutableBytesPtr(), anonymousCipherText.bytesPtr(), CUnsignedLongLong(anonymousCipherText.length), recipientPublicKey.bytesPtr(), recipientSecretKey.bytesPtr()) != 0 { return nil } return message diff --git a/Sodium/GenericHash.swift b/Sodium/GenericHash.swift index 5a6217f..d0d3c93 100644 --- a/Sodium/GenericHash.swift +++ b/Sodium/GenericHash.swift @@ -27,9 +27,9 @@ public class GenericHash { } var ret: CInt; if let key = key { - ret = crypto_generichash(output.mutableBytesPtr, output.length, message.bytesPtr, CUnsignedLongLong(message.length), key.bytesPtr, key.length) + ret = crypto_generichash(output.mutableBytesPtr(), output.length, message.bytesPtr(), CUnsignedLongLong(message.length), key.bytesPtr(), key.length) } else { - ret = crypto_generichash(output.mutableBytesPtr, output.length, message.bytesPtr, CUnsignedLongLong(message.length), nil, 0) + ret = crypto_generichash(output.mutableBytesPtr(), output.length, message.bytesPtr(), CUnsignedLongLong(message.length), nil, 0) } if ret != 0 { return nil @@ -64,7 +64,7 @@ public class GenericHash { } var ret: CInt if let key = key { - ret = crypto_generichash_init(state, key.bytesPtr, key.length, outputLength) + ret = crypto_generichash_init(state, key.bytesPtr(), key.length, outputLength) } else { ret = crypto_generichash_init(state, nil, 0, outputLength) } @@ -79,14 +79,14 @@ public class GenericHash { } public func update(input: NSData) -> Bool { - return crypto_generichash_update(state!, input.bytesPtr, CUnsignedLongLong(input.length)) == 0 + return crypto_generichash_update(state!, input.bytesPtr(), CUnsignedLongLong(input.length)) == 0 } public func final() -> NSData? { guard let output = NSMutableData(length: outputLength) else { return nil } - if crypto_generichash_final(state!, output.mutableBytesPtr, output.length) != 0 { + if crypto_generichash_final(state!, output.mutableBytesPtr(), output.length) != 0 { return nil } return output diff --git a/Sodium/NSData+Extensions.swift b/Sodium/NSData+Extensions.swift index e0c028a..926c6a6 100644 --- a/Sodium/NSData+Extensions.swift +++ b/Sodium/NSData+Extensions.swift @@ -8,14 +8,16 @@ import Foundation -public extension NSData { - var bytesPtr: UnsafePointer { - return UnsafePointer(self.bytes) +public extension NSData { + func bytesPtr() -> UnsafePointer{ + let rawBytes = self.bytes + return rawBytes.assumingMemoryBound(to: T.self); } } public extension NSMutableData { - var mutableBytesPtr: UnsafeMutablePointer { - return UnsafeMutablePointer(self.mutableBytes) + func mutableBytesPtr() -> UnsafeMutablePointer{ + let rawBytes = self.mutableBytes + return rawBytes.assumingMemoryBound(to: T.self) } } diff --git a/Sodium/PWHash.swift b/Sodium/PWHash.swift index 7c19976..0b77292 100644 --- a/Sodium/PWHash.swift +++ b/Sodium/PWHash.swift @@ -23,7 +23,8 @@ public class PWHash { guard let output = NSMutableData(length: StrBytes) else { return nil } - if crypto_pwhash_str(UnsafeMutablePointer(output.mutableBytes), UnsafePointer(passwd.bytes), CUnsignedLongLong(passwd.length), CUnsignedLongLong(opsLimit), size_t(memLimit)) != 0 { + let outputRawBytes = output.mutableBytes + if crypto_pwhash_str(outputRawBytes.assumingMemoryBound(to: CChar.self), passwd.bytesPtr(), CUnsignedLongLong(passwd.length), CUnsignedLongLong(opsLimit), size_t(memLimit)) != 0 { return nil } return NSString(data: output as Data, encoding: String.Encoding.utf8.rawValue) as String? @@ -33,7 +34,7 @@ public class PWHash { guard let hashData = (hash + "\0").data(using: String.Encoding.utf8, allowLossyConversion: false) else { return false } - return crypto_pwhash_str_verify(UnsafePointer((hashData as NSData).bytes), UnsafePointer(passwd.bytes), CUnsignedLongLong(passwd.length)) == 0 + return crypto_pwhash_str_verify((hashData as NSData).bytesPtr(), passwd.bytesPtr(), CUnsignedLongLong(passwd.length)) == 0 } public func hash(outputLength: Int, passwd: NSData, salt: NSData, opsLimit: Int, memLimit: Int) -> NSData? { @@ -43,7 +44,7 @@ public class PWHash { guard let output = NSMutableData(length: outputLength) else { return nil } - if crypto_pwhash(output.mutableBytesPtr, CUnsignedLongLong(outputLength), UnsafePointer(passwd.bytes), CUnsignedLongLong(passwd.length), salt.bytesPtr, CUnsignedLongLong(opsLimit), size_t(memLimit), crypto_pwhash_ALG_DEFAULT) != 0 { + if crypto_pwhash(output.mutableBytesPtr(), CUnsignedLongLong(outputLength), passwd.bytesPtr(), CUnsignedLongLong(passwd.length), salt.bytesPtr(), CUnsignedLongLong(opsLimit), size_t(memLimit), crypto_pwhash_ALG_DEFAULT) != 0 { return nil } return output @@ -64,7 +65,7 @@ public class PWHash { guard let output = NSMutableData(length: StrBytes) else { return nil } - if crypto_pwhash_scryptsalsa208sha256_str(UnsafeMutablePointer(output.mutableBytes), UnsafePointer(passwd.bytes), CUnsignedLongLong(passwd.length), CUnsignedLongLong(opsLimit), size_t(memLimit)) != 0 { + if crypto_pwhash_scryptsalsa208sha256_str(output.mutableBytesPtr(), passwd.bytesPtr(), CUnsignedLongLong(passwd.length), CUnsignedLongLong(opsLimit), size_t(memLimit)) != 0 { return nil } return NSString(data: output as Data, encoding: String.Encoding.utf8.rawValue) as String? @@ -74,7 +75,7 @@ public class PWHash { guard let hashData = (hash + "\0").data(using: String.Encoding.utf8, allowLossyConversion: false) else { return false } - return crypto_pwhash_scryptsalsa208sha256_str_verify(UnsafePointer((hashData as NSData).bytes), UnsafePointer(passwd.bytes), CUnsignedLongLong(passwd.length)) == 0 + return crypto_pwhash_scryptsalsa208sha256_str_verify((hashData as NSData).bytesPtr(), passwd.bytesPtr(), CUnsignedLongLong(passwd.length)) == 0 } public func hash(outputLength: Int, passwd: NSData, salt: NSData, opsLimit: Int, memLimit: Int) -> NSData? { @@ -84,7 +85,7 @@ public class PWHash { guard let output = NSMutableData(length: outputLength) else { return nil } - if crypto_pwhash_scryptsalsa208sha256(output.mutableBytesPtr, CUnsignedLongLong(outputLength), UnsafePointer(passwd.bytes), CUnsignedLongLong(passwd.length), salt.bytesPtr, CUnsignedLongLong(opsLimit), size_t(memLimit)) != 0 { + if crypto_pwhash_scryptsalsa208sha256(output.mutableBytesPtr(), CUnsignedLongLong(outputLength), passwd.bytesPtr(), CUnsignedLongLong(passwd.length), salt.bytesPtr(), CUnsignedLongLong(opsLimit), size_t(memLimit)) != 0 { return nil } return output diff --git a/Sodium/RandomBytes.swift b/Sodium/RandomBytes.swift index 66fc02c..24b1dec 100644 --- a/Sodium/RandomBytes.swift +++ b/Sodium/RandomBytes.swift @@ -16,7 +16,7 @@ public class RandomBytes { guard let output = NSMutableData(length: length) else { return nil } - randombytes_buf(output.mutableBytesPtr, output.length) + randombytes_buf(output.mutableBytesPtr(), output.length) return output } diff --git a/Sodium/SecretBox.swift b/Sodium/SecretBox.swift index 3471e7e..bcc3365 100644 --- a/Sodium/SecretBox.swift +++ b/Sodium/SecretBox.swift @@ -21,7 +21,7 @@ public class SecretBox { guard let k = NSMutableData(length: KeyBytes) else { return nil } - randombytes_buf(k.mutableBytesPtr, k.length) + randombytes_buf(k.mutableBytesPtr(), k.length) return k } @@ -29,7 +29,7 @@ public class SecretBox { guard let n = NSMutableData(length: NonceBytes) else { return nil } - randombytes_buf(n.mutableBytesPtr, n.length) + randombytes_buf(n.mutableBytesPtr(), n.length) return n } @@ -52,7 +52,7 @@ public class SecretBox { guard let nonce = self.nonce() else { return nil } - if crypto_secretbox_easy(authenticatedCipherText.mutableBytesPtr, message.bytesPtr, UInt64(message.length), nonce.bytesPtr, secretKey.bytesPtr) != 0 { + if crypto_secretbox_easy(authenticatedCipherText.mutableBytesPtr(), message.bytesPtr(), UInt64(message.length), nonce.bytesPtr(), secretKey.bytesPtr()) != 0 { return nil } return (authenticatedCipherText: authenticatedCipherText, nonce: nonce) @@ -71,7 +71,7 @@ public class SecretBox { guard let nonce = self.nonce() else { return nil } - if crypto_secretbox_detached(cipherText.mutableBytesPtr, mac.mutableBytesPtr, message.bytesPtr, UInt64(message.length), nonce.bytesPtr, secretKey.bytesPtr) != 0 { + if crypto_secretbox_detached(cipherText.mutableBytesPtr(), mac.mutableBytesPtr(), message.bytesPtr(), UInt64(message.length), nonce.bytesPtr(), secretKey.bytesPtr()) != 0 { return nil } return (cipherText: cipherText, nonce: nonce, mac: mac) @@ -96,7 +96,7 @@ public class SecretBox { guard let message = NSMutableData(length: authenticatedCipherText.length - MacBytes) else { return nil } - if crypto_secretbox_open_easy(message.mutableBytesPtr, authenticatedCipherText.bytesPtr, UInt64(authenticatedCipherText.length), nonce.bytesPtr, secretKey.bytesPtr) != 0 { + if crypto_secretbox_open_easy(message.mutableBytesPtr(), authenticatedCipherText.bytesPtr(), UInt64(authenticatedCipherText.length), nonce.bytesPtr(), secretKey.bytesPtr()) != 0 { return nil } return message @@ -112,7 +112,7 @@ public class SecretBox { guard let message = NSMutableData(length: cipherText.length) else { return nil } - if crypto_secretbox_open_detached(message.mutableBytesPtr, cipherText.bytesPtr, mac.bytesPtr, UInt64(cipherText.length), nonce.bytesPtr, secretKey.bytesPtr) != 0 { + if crypto_secretbox_open_detached(message.mutableBytesPtr(), cipherText.bytesPtr(), mac.bytesPtr(), UInt64(cipherText.length), nonce.bytesPtr(), secretKey.bytesPtr()) != 0 { return nil } return message diff --git a/Sodium/ShortHash.swift b/Sodium/ShortHash.swift index a6d235f..28ef41d 100644 --- a/Sodium/ShortHash.swift +++ b/Sodium/ShortHash.swift @@ -19,7 +19,7 @@ public class ShortHash { guard let output = NSMutableData(length: Bytes) else { return nil } - if crypto_shorthash(output.mutableBytesPtr, message.bytesPtr, CUnsignedLongLong(message.length), key.bytesPtr) != 0 { + if crypto_shorthash(output.mutableBytesPtr(), message.bytesPtr(), CUnsignedLongLong(message.length), key.bytesPtr()) != 0 { return nil } return output diff --git a/Sodium/Sign.swift b/Sodium/Sign.swift index 1f36128..2499f77 100644 --- a/Sodium/Sign.swift +++ b/Sodium/Sign.swift @@ -35,7 +35,7 @@ public class Sign { guard let sk = NSMutableData(length: SecretKeyBytes) else { return nil } - if crypto_sign_keypair(pk.mutableBytesPtr, sk.mutableBytesPtr) != 0 { + if crypto_sign_keypair(pk.mutableBytesPtr(), sk.mutableBytesPtr()) != 0 { return nil } return KeyPair(publicKey: PublicKey(data: pk as Data), secretKey: SecretKey(data: sk as Data)) @@ -51,7 +51,7 @@ public class Sign { guard let sk = NSMutableData(length: SecretKeyBytes) else { return nil } - if crypto_sign_seed_keypair(pk.mutableBytesPtr, sk.mutableBytesPtr, seed.bytesPtr) != 0 { + if crypto_sign_seed_keypair(pk.mutableBytesPtr(), sk.mutableBytesPtr(), seed.bytesPtr()) != 0 { return nil } return KeyPair(publicKey: PublicKey(data: pk as Data), secretKey: SecretKey(data: sk as Data)) @@ -64,7 +64,7 @@ public class Sign { guard let signedMessage = NSMutableData(length: message.length + Bytes) else { return nil } - if crypto_sign(signedMessage.mutableBytesPtr, nil, message.bytesPtr, CUnsignedLongLong(message.length), secretKey.bytesPtr) != 0 { + if crypto_sign(signedMessage.mutableBytesPtr(), nil, message.bytesPtr(), CUnsignedLongLong(message.length), secretKey.bytesPtr()) != 0 { return nil } return signedMessage @@ -77,7 +77,7 @@ public class Sign { guard let signature = NSMutableData(length: Bytes) else { return nil } - if crypto_sign_detached(signature.mutableBytesPtr, nil, message.bytesPtr, CUnsignedLongLong(message.length), secretKey.bytesPtr) != 0 { + if crypto_sign_detached(signature.mutableBytesPtr(), nil, message.bytesPtr(), CUnsignedLongLong(message.length), secretKey.bytesPtr()) != 0 { return nil } return signature @@ -93,7 +93,7 @@ public class Sign { if publicKey.length != PublicKeyBytes { return false } - return crypto_sign_verify_detached(signature.bytesPtr, message.bytesPtr, CUnsignedLongLong(message.length), publicKey.bytesPtr) == 0 + return crypto_sign_verify_detached(signature.bytesPtr(), message.bytesPtr(), CUnsignedLongLong(message.length), publicKey.bytesPtr()) == 0 } public func open(signedMessage: NSData, publicKey: PublicKey) -> NSData? { @@ -104,7 +104,7 @@ public class Sign { return nil } var mlen: CUnsignedLongLong = 0; - if crypto_sign_open(message.mutableBytesPtr, &mlen, signedMessage.bytesPtr, CUnsignedLongLong(signedMessage.length), publicKey.bytesPtr) != 0 { + if crypto_sign_open(message.mutableBytesPtr(), &mlen, signedMessage.bytesPtr(), CUnsignedLongLong(signedMessage.length), publicKey.bytesPtr()) != 0 { return nil } return message diff --git a/Sodium/Utils.swift b/Sodium/Utils.swift index 801c16f..61f5dbb 100644 --- a/Sodium/Utils.swift +++ b/Sodium/Utils.swift @@ -10,7 +10,7 @@ import Foundation public class Utils { public func zero(data: NSMutableData) { - sodium_memzero(UnsafeMutablePointer(data.mutableBytes), data.length) + sodium_memzero(UnsafeMutableRawPointer(data.mutableBytes), data.length) data.length = 0 } @@ -18,7 +18,7 @@ public class Utils { if b1.length != b2.length { return false } - let res = sodium_memcmp(UnsafePointer(b1.bytes), UnsafePointer(b2.bytes), b1.length) + let res = sodium_memcmp(UnsafeRawPointer(b1.bytes), UnsafeRawPointer(b2.bytes), b1.length) return res == 0; } @@ -26,7 +26,7 @@ public class Utils { if b1.length != b2.length { return nil } - let res = sodium_compare(b1.bytesPtr, b2.bytesPtr, b1.length) + let res = sodium_compare(b1.bytesPtr(), b2.bytesPtr(), b1.length) return Int(res); } @@ -34,11 +34,10 @@ public class Utils { guard let hexData = NSMutableData(length: bin.length * 2 + 1) else { return nil } - let hexDataBytes = UnsafeMutablePointer(hexData.mutableBytes) - if sodium_bin2hex(hexDataBytes, hexData.length, bin.bytesPtr, bin.length) == nil { + if sodium_bin2hex(hexData.mutableBytesPtr(), hexData.length, bin.bytesPtr(), bin.length) == nil { return nil } - return String.init(validatingUTF8: hexDataBytes) + return String.init(validatingUTF8: hexData.mutableBytesPtr()) } public func hex2bin(hex: String, ignore: String? = nil) -> NSData? { @@ -52,7 +51,7 @@ public class Utils { } var binDataLen: size_t = 0 let ignore_cstr = ignore != nil ? (ignore! as NSString).utf8String : nil - if sodium_hex2bin(binData.mutableBytesPtr, binDataCapacity,UnsafePointer((hexData as NSData).bytes), hexDataLen, ignore_cstr, &binDataLen, nil) != 0 { + if sodium_hex2bin(binData.mutableBytesPtr(), binDataCapacity,(hexData as NSData).bytesPtr(), hexDataLen, ignore_cstr, &binDataLen, nil) != 0 { return nil } binData.length = Int(binDataLen) diff --git a/SodiumTests/SodiumTests.swift b/SodiumTests/SodiumTests.swift index 6683979..fbd213d 100644 --- a/SodiumTests/SodiumTests.swift +++ b/SodiumTests/SodiumTests.swift @@ -11,7 +11,7 @@ import Sodium extension String { func toData() -> NSData? { - return self.data(using: String.Encoding.utf8, allowLossyConversion: false) + return self.data(using: String.Encoding.utf8, allowLossyConversion: false) as NSData? } }