Digest.swift 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. //
  2. // Hash.swift
  3. // CryptoSwift
  4. //
  5. // Created by Marcin Krzyzanowski on 07/08/14.
  6. // Copyright (c) 2014 Marcin Krzyzanowski. All rights reserved.
  7. //
  8. @available(*, deprecated: 0.6.0, renamed: "Digest")
  9. public typealias Hash = Digest
  10. /// Hash functions to calculate Digest.
  11. public struct Digest {
  12. /// Calculate MD5 Digest
  13. /// - parameter bytes: input message
  14. /// - returns: Digest bytes
  15. public static func md5(_ bytes: Array<UInt8>) -> Array<UInt8> {
  16. return MD5().calculate(for: bytes)
  17. }
  18. /// Calculate SHA1 Digest
  19. /// - parameter bytes: input message
  20. /// - returns: Digest bytes
  21. public static func sha1(_ bytes: Array<UInt8>) -> Array<UInt8> {
  22. return SHA1().calculate(for: bytes)
  23. }
  24. /// Calculate SHA2-224 Digest
  25. /// - parameter bytes: input message
  26. /// - returns: Digest bytes
  27. public static func sha224(_ bytes: Array<UInt8>) -> Array<UInt8> {
  28. return sha2(bytes, variant: .sha224)
  29. }
  30. /// Calculate SHA2-256 Digest
  31. /// - parameter bytes: input message
  32. /// - returns: Digest bytes
  33. public static func sha256(_ bytes: Array<UInt8>) -> Array<UInt8> {
  34. return sha2(bytes, variant: .sha256)
  35. }
  36. /// Calculate SHA2-384 Digest
  37. /// - parameter bytes: input message
  38. /// - returns: Digest bytes
  39. public static func sha384(_ bytes: Array<UInt8>) -> Array<UInt8> {
  40. return sha2(bytes, variant: .sha384)
  41. }
  42. /// Calculate SHA2-512 Digest
  43. /// - parameter bytes: input message
  44. /// - returns: Digest bytes
  45. public static func sha512(_ bytes: Array<UInt8>) -> Array<UInt8> {
  46. return sha2(bytes, variant: .sha512)
  47. }
  48. /// Calculate SHA2 Digest
  49. /// - parameter bytes: input message
  50. /// - parameter variant: SHA-2 variant
  51. /// - returns: Digest bytes
  52. public static func sha2(_ bytes: Array<UInt8>, variant: SHA2.Variant) -> Array<UInt8> {
  53. return SHA2(variant: variant).calculate(for: bytes)
  54. }
  55. /// Calculate SHA3 Digest
  56. /// - parameter bytes: input message
  57. /// - parameter variant: SHA-3 variant
  58. /// - returns: Digest bytes
  59. public static func sha3(_ bytes: Array<UInt8>, variant: SHA3.Variant) -> Array<UInt8> {
  60. return SHA3(variant: variant).calculate(for: bytes)
  61. }
  62. }