Cryptors.swift 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. //
  2. // Cryptors.swift
  3. // CryptoSwift
  4. //
  5. // Created by Marcin Krzyzanowski on 30/08/14.
  6. // Copyright (c) 2014 Marcin Krzyzanowski. All rights reserved.
  7. //
  8. #if os(Linux) || os(Android) || os(FreeBSD)
  9. import Glibc
  10. #else
  11. import Darwin
  12. #endif
  13. /// Worker cryptor/decryptor of `Updatable` types
  14. public protocol Cryptors: class {
  15. associatedtype EncryptorType: Updatable
  16. associatedtype DecryptorType: Updatable
  17. /// Cryptor suitable for encryption
  18. func makeEncryptor() -> EncryptorType
  19. /// Cryptor suitable for decryption
  20. func makeDecryptor() -> DecryptorType
  21. /// Generate array of random bytes. Helper function.
  22. static func randomIV(_ blockSize: Int) -> Array<UInt8>
  23. }
  24. extension Cryptors {
  25. public static func randomIV(_ blockSize: Int) -> Array<UInt8> {
  26. var randomIV: Array<UInt8> = Array<UInt8>()
  27. randomIV.reserveCapacity(blockSize)
  28. for randomByte in RandomBytesSequence(size: blockSize) {
  29. randomIV.append(randomByte)
  30. }
  31. return randomIV
  32. }
  33. }