Cipher.swift 785 B

1234567891011121314151617181920212223242526272829303132333435
  1. //
  2. // Cipher.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)
  9. import Glibc
  10. #else
  11. import Darwin
  12. #endif
  13. public enum CipherError: ErrorType {
  14. case Encrypt
  15. case Decrypt
  16. }
  17. public protocol Cipher {
  18. func cipherEncrypt(bytes: [UInt8]) throws -> [UInt8]
  19. func cipherDecrypt(bytes: [UInt8]) throws -> [UInt8]
  20. static func randomIV(blockSize:Int) -> [UInt8]
  21. }
  22. extension Cipher {
  23. static public func randomIV(blockSize:Int) -> [UInt8] {
  24. var randomIV:[UInt8] = [UInt8]();
  25. for (var i = 0; i < blockSize; i++) {
  26. randomIV.append(UInt8(truncatingBitPattern: cs_arc4random_uniform(256)));
  27. }
  28. return randomIV
  29. }
  30. }