Digest.swift 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. //
  2. // CryptoSwift
  3. //
  4. // Copyright (C) 2014-2017 Marcin Krzyżanowski <marcin@krzyzanowskim.com>
  5. // This software is provided 'as-is', without any express or implied warranty.
  6. //
  7. // In no event will the authors be held liable for any damages arising from the use of this software.
  8. //
  9. // Permission is granted to anyone to use this software for any purpose,including commercial applications, and to alter it and redistribute it freely, subject to the following restrictions:
  10. //
  11. // - The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation is required.
  12. // - Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
  13. // - This notice may not be removed or altered from any source or binary distribution.
  14. //
  15. @available(*, renamed: "Digest")
  16. public typealias Hash = Digest
  17. /// Hash functions to calculate Digest.
  18. public struct Digest {
  19. /// Calculate MD5 Digest
  20. /// - parameter bytes: input message
  21. /// - returns: Digest bytes
  22. public static func md5(_ bytes: Array<UInt8>) -> Array<UInt8> {
  23. MD5().calculate(for: bytes)
  24. }
  25. /// Calculate SHA1 Digest
  26. /// - parameter bytes: input message
  27. /// - returns: Digest bytes
  28. public static func sha1(_ bytes: Array<UInt8>) -> Array<UInt8> {
  29. SHA1().calculate(for: bytes)
  30. }
  31. /// Calculate SHA2-224 Digest
  32. /// - parameter bytes: input message
  33. /// - returns: Digest bytes
  34. public static func sha224(_ bytes: Array<UInt8>) -> Array<UInt8> {
  35. self.sha2(bytes, variant: .sha224)
  36. }
  37. /// Calculate SHA2-256 Digest
  38. /// - parameter bytes: input message
  39. /// - returns: Digest bytes
  40. public static func sha256(_ bytes: Array<UInt8>) -> Array<UInt8> {
  41. self.sha2(bytes, variant: .sha256)
  42. }
  43. /// Calculate SHA2-384 Digest
  44. /// - parameter bytes: input message
  45. /// - returns: Digest bytes
  46. public static func sha384(_ bytes: Array<UInt8>) -> Array<UInt8> {
  47. self.sha2(bytes, variant: .sha384)
  48. }
  49. /// Calculate SHA2-512 Digest
  50. /// - parameter bytes: input message
  51. /// - returns: Digest bytes
  52. public static func sha512(_ bytes: Array<UInt8>) -> Array<UInt8> {
  53. self.sha2(bytes, variant: .sha512)
  54. }
  55. /// Calculate SHA2 Digest
  56. /// - parameter bytes: input message
  57. /// - parameter variant: SHA-2 variant
  58. /// - returns: Digest bytes
  59. public static func sha2(_ bytes: Array<UInt8>, variant: SHA2.Variant) -> Array<UInt8> {
  60. SHA2(variant: variant).calculate(for: bytes)
  61. }
  62. /// Calculate SHA3 Digest
  63. /// - parameter bytes: input message
  64. /// - parameter variant: SHA-3 variant
  65. /// - returns: Digest bytes
  66. public static func sha3(_ bytes: Array<UInt8>, variant: SHA3.Variant) -> Array<UInt8> {
  67. SHA3(variant: variant).calculate(for: bytes)
  68. }
  69. }