ZeroPadding.swift 959 B

1234567891011121314151617181920212223242526272829303132
  1. //
  2. // ZeroPadding.swift
  3. // CryptoSwift
  4. //
  5. // Created by Marcin Krzyzanowski on 13/06/16.
  6. // Copyright © 2016 Marcin Krzyzanowski. All rights reserved.
  7. //
  8. /// All the bytes that are required to be padded are padded with zero.
  9. /// Zero padding may not be reversible if the original file ends with one or more zero bytes.
  10. public struct ZeroPadding: Padding {
  11. public init() {
  12. }
  13. public func add(to bytes: Array<UInt8>, blockSize:Int) -> Array<UInt8> {
  14. let paddingCount = blockSize - (bytes.count % blockSize)
  15. if paddingCount > 0 {
  16. return bytes + Array<UInt8>(repeating: 0, count: paddingCount)
  17. }
  18. return bytes
  19. }
  20. public func remove(from bytes: Array<UInt8>, blockSize:Int?) -> Array<UInt8> {
  21. for (idx, value) in bytes.reversed().enumerated() {
  22. if value != 0 {
  23. return Array(bytes[0..<bytes.count - idx])
  24. }
  25. }
  26. return bytes;
  27. }
  28. }