Array<UInt8>+Extension.swift 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. //
  2. // ArrayUInt8+CryptoSwift.swift
  3. // CryptoSwift
  4. //
  5. // Created by Marcin Krzyzanowski on 27/09/15.
  6. // Copyright © 2015 Marcin Krzyzanowski. All rights reserved.
  7. //
  8. extension Array where Element: _UInt8Type {
  9. public func toHexString() -> String {
  10. return self.lazy.reduce("") { $0 + String(format:"%02x", $1 as! UInt8) }
  11. }
  12. public func md5() -> [UInt8]? {
  13. return Hash.md5(Element.arrayValue(self)).calculate()
  14. }
  15. public func sha1() -> [UInt8]? {
  16. return Hash.sha1(Element.arrayValue(self)).calculate()
  17. }
  18. public func sha224() -> [UInt8]? {
  19. return Hash.sha224(Element.arrayValue(self)).calculate()
  20. }
  21. public func sha256() -> [UInt8]? {
  22. return Hash.sha256(Element.arrayValue(self)).calculate()
  23. }
  24. public func sha384() -> [UInt8]? {
  25. return Hash.sha384(Element.arrayValue(self)).calculate()
  26. }
  27. public func sha512() -> [UInt8]? {
  28. return Hash.sha512(Element.arrayValue(self)).calculate()
  29. }
  30. public func crc32() -> [UInt8]? {
  31. return Hash.crc32(Element.arrayValue(self)).calculate()
  32. }
  33. public func encrypt(cipher: Cipher) throws -> [UInt8]? {
  34. return try cipher.encrypt(Element.arrayValue(self))
  35. }
  36. public func decrypt(cipher: Cipher) throws -> [UInt8]? {
  37. return try cipher.decrypt(Element.arrayValue(self))
  38. }
  39. public func authenticate(authenticator: Authenticator) -> [UInt8]? {
  40. return authenticator.authenticate(Element.arrayValue(self))
  41. }
  42. }