HashProtocol.swift 960 B

123456789101112131415161718192021222324252627282930313233343536
  1. //
  2. // HashProtocol.swift
  3. // CryptoSwift
  4. //
  5. // Created by Marcin Krzyzanowski on 17/08/14.
  6. // Copyright (c) 2014 Marcin Krzyzanowski. All rights reserved.
  7. //
  8. internal protocol HashProtocol: class {
  9. var message: Array<UInt8> { get }
  10. /** Common part for hash calculation. Prepare header data. */
  11. func prepare(_ len:Int) -> Array<UInt8>
  12. }
  13. extension HashProtocol {
  14. func prepare(_ len:Int) -> Array<UInt8> {
  15. var tmpMessage = message
  16. // Step 1. Append Padding Bits
  17. tmpMessage.append(0x80) // append one bit (UInt8 with one bit) to message
  18. // append "0" bit until message length in bits ≡ 448 (mod 512)
  19. var msgLength = tmpMessage.count
  20. var counter = 0
  21. while msgLength % len != (len - 8) {
  22. counter += 1
  23. msgLength += 1
  24. }
  25. tmpMessage += Array<UInt8>(repeating: 0, count: counter)
  26. return tmpMessage
  27. }
  28. }