Hash.swift 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. // Hash.swift
  2. // This file is part of KeePassKit.
  3. //
  4. // Copyright © 2019 Maxime Epain. All rights reserved.
  5. //
  6. // KeePassKit is free software: you can redistribute it and/or modify
  7. // it under the terms of the GNU General Public License as published by
  8. // the Free Software Foundation, either version 3 of the License, or
  9. // (at your option) any later version.
  10. //
  11. // KeePassKit is distributed in the hope that it will be useful,
  12. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. // GNU General Public License for more details.
  15. //
  16. // You should have received a copy of the GNU General Public License
  17. // along with KeePassKit. If not, see <https://www.gnu.org/licenses/>.
  18. import Foundation
  19. import Binary
  20. import Sodium
  21. public final class SHA256 {
  22. public static let Lenght = Int(crypto_hash_sha256_BYTES)
  23. private var state: crypto_hash_sha256_state
  24. public var final: Bytes {
  25. var out = Bytes(lenght: SHA256.Lenght)
  26. crypto_hash_sha256_final(&state, &out.rawValue)
  27. return out
  28. }
  29. public init() {
  30. state = crypto_hash_sha256_state()
  31. crypto_hash_sha256_init(&state)
  32. }
  33. public func update(_ bytes: Bytes) {
  34. crypto_hash_sha256_update(&state, bytes.rawValue, lenght(bytes));
  35. }
  36. public static func hash(_ bytes: Bytes) -> Bytes {
  37. var out = Bytes(lenght: SHA256.Lenght)
  38. crypto_hash_sha256(&out.rawValue, bytes.rawValue, lenght(bytes))
  39. return out
  40. }
  41. }
  42. public final class SHA512 {
  43. public static let Lenght = Int(crypto_hash_sha512_BYTES)
  44. private var state: crypto_hash_sha512_state
  45. public var final: Bytes {
  46. var out = Bytes(lenght: SHA512.Lenght)
  47. crypto_hash_sha512_final(&state, &out.rawValue)
  48. return out
  49. }
  50. public init() {
  51. state = crypto_hash_sha512_state()
  52. crypto_hash_sha512_init(&state)
  53. }
  54. public func update(_ bytes: Bytes) {
  55. crypto_hash_sha512_update(&state, bytes.rawValue, lenght(bytes));
  56. }
  57. public static func hash(_ bytes: Bytes) -> Bytes {
  58. var out = Bytes(lenght: SHA512.Lenght)
  59. crypto_hash_sha512(&out.rawValue, bytes.rawValue, lenght(bytes))
  60. return out
  61. }
  62. }
  63. public final class HMACSHA256 {
  64. public static let Lenght = Int(crypto_auth_hmacsha256_BYTES)
  65. private var state: crypto_auth_hmacsha256_state
  66. public var final: Bytes {
  67. var out = Bytes(lenght: HMACSHA256.Lenght)
  68. crypto_auth_hmacsha256_final(&state, &out.rawValue)
  69. return out
  70. }
  71. public init(key: Bytes) {
  72. state = crypto_auth_hmacsha256_state()
  73. crypto_auth_hmacsha256_init(&state, key.rawValue, lenght(key))
  74. }
  75. public func update(_ bytes: Bytes) {
  76. crypto_auth_hmacsha256_update(&state, bytes.rawValue, lenght(bytes));
  77. }
  78. public static func authenticate(_ bytes: Bytes, key: Bytes) -> Bytes {
  79. let hmac = HMACSHA256(key: key)
  80. hmac.update(bytes)
  81. return hmac.final
  82. }
  83. }