Hash.swift 1.2 KB

123456789101112131415161718192021222324252627282930313233343536
  1. //
  2. // CryptoHash.swift
  3. // CryptoSwift
  4. //
  5. // Created by Marcin Krzyzanowski on 07/08/14.
  6. // Copyright (c) 2014 Marcin Krzyzanowski. All rights reserved.
  7. //
  8. public enum Hash {
  9. case md5(Array<UInt8>)
  10. case sha1(Array<UInt8>)
  11. case sha224(Array<UInt8>), sha256(Array<UInt8>), sha384(Array<UInt8>), sha512(Array<UInt8>)
  12. case crc32(Array<UInt8>, seed: UInt32?, reflect: Bool)
  13. case crc16(Array<UInt8>, seed: UInt16?)
  14. public func calculate() -> Array<UInt8> {
  15. switch self {
  16. case md5(let bytes):
  17. return MD5(bytes).calculate()
  18. case sha1(let bytes):
  19. return SHA1(bytes).calculate()
  20. case sha224(let bytes):
  21. return SHA2(bytes, variant: .sha224).calculate32()
  22. case sha256(let bytes):
  23. return SHA2(bytes, variant: .sha256).calculate32()
  24. case sha384(let bytes):
  25. return SHA2(bytes, variant: .sha384).calculate64()
  26. case sha512(let bytes):
  27. return SHA2(bytes, variant: .sha512).calculate64()
  28. case crc32(let bytes):
  29. return CRC().crc32(bytes.0, seed: bytes.seed, reflect: bytes.reflect).bytes()
  30. case crc16(let bytes):
  31. return UInt32(CRC().crc16(bytes.0, seed: bytes.seed)).bytes(2)
  32. }
  33. }
  34. }