-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDecrypter.swift
More file actions
25 lines (18 loc) · 865 Bytes
/
Decrypter.swift
File metadata and controls
25 lines (18 loc) · 865 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import Security
import Foundation
class Decrypter {
static func decrypt(encryptedData: [UInt8], privateKey: SecKey, encoding: NSStringEncoding = NSUTF8StringEncoding) throws -> String {
let blockSize = SecKeyGetBlockSize(privateKey)
var decryptedData = [UInt8](count: Int(blockSize), repeatedValue: 0)
var decryptedDataLength = blockSize
let result = SecKeyDecrypt(privateKey, SecPadding(arrayLiteral: SecPadding.PKCS1), encryptedData, decryptedDataLength, &decryptedData, &decryptedDataLength)
guard result == errSecSuccess else {
throw EncryptionError.DecryptionFailed(code: result)
}
if let decryptedText = String(bytes: decryptedData, encoding: encoding) {
return decryptedText
} else {
throw EncryptionError.TextDecodingFailed
}
}
}