Utils.swift 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. @inlinable
  16. func rotateLeft(_ value: UInt8, by: UInt8) -> UInt8 {
  17. ((value << by) & 0xff) | (value >> (8 - by))
  18. }
  19. @inlinable
  20. func rotateLeft(_ value: UInt16, by: UInt16) -> UInt16 {
  21. ((value << by) & 0xffff) | (value >> (16 - by))
  22. }
  23. @inlinable
  24. func rotateLeft(_ value: UInt32, by: UInt32) -> UInt32 {
  25. ((value << by) & 0xffffffff) | (value >> (32 - by))
  26. }
  27. @inlinable
  28. func rotateLeft(_ value: UInt64, by: UInt64) -> UInt64 {
  29. (value << by) | (value >> (64 - by))
  30. }
  31. @inlinable
  32. func rotateRight(_ value: UInt16, by: UInt16) -> UInt16 {
  33. (value >> by) | (value << (16 - by))
  34. }
  35. @inlinable
  36. func rotateRight(_ value: UInt32, by: UInt32) -> UInt32 {
  37. (value >> by) | (value << (32 - by))
  38. }
  39. @inlinable
  40. func rotateRight(_ value: UInt64, by: UInt64) -> UInt64 {
  41. ((value >> by) | (value << (64 - by)))
  42. }
  43. @inlinable
  44. func reversed(_ uint8: UInt8) -> UInt8 {
  45. var v = uint8
  46. v = (v & 0xf0) >> 4 | (v & 0x0f) << 4
  47. v = (v & 0xcc) >> 2 | (v & 0x33) << 2
  48. v = (v & 0xaa) >> 1 | (v & 0x55) << 1
  49. return v
  50. }
  51. @inlinable
  52. func reversed(_ uint32: UInt32) -> UInt32 {
  53. var v = uint32
  54. v = ((v >> 1) & 0x55555555) | ((v & 0x55555555) << 1)
  55. v = ((v >> 2) & 0x33333333) | ((v & 0x33333333) << 2)
  56. v = ((v >> 4) & 0x0f0f0f0f) | ((v & 0x0f0f0f0f) << 4)
  57. v = ((v >> 8) & 0x00ff00ff) | ((v & 0x00ff00ff) << 8)
  58. v = ((v >> 16) & 0xffff) | ((v & 0xffff) << 16)
  59. return v
  60. }
  61. @inlinable
  62. func xor<T, V>(_ left: T, _ right: V) -> ArraySlice<UInt8> where T: RandomAccessCollection, V: RandomAccessCollection, T.Element == UInt8, T.Index == Int, V.Element == UInt8, V.Index == Int {
  63. return xor(left, right).slice
  64. }
  65. @inlinable
  66. func xor<T, V>(_ left: T, _ right: V) -> Array<UInt8> where T: RandomAccessCollection, V: RandomAccessCollection, T.Element == UInt8, T.Index == Int, V.Element == UInt8, V.Index == Int {
  67. let length = Swift.min(left.count, right.count)
  68. let buf = UnsafeMutablePointer<UInt8>.allocate(capacity: length)
  69. buf.initialize(repeating: 0, count: length)
  70. defer {
  71. buf.deinitialize(count: length)
  72. buf.deallocate()
  73. }
  74. // xor
  75. for i in 0..<length {
  76. buf[i] = left[left.startIndex.advanced(by: i)] ^ right[right.startIndex.advanced(by: i)]
  77. }
  78. return Array(UnsafeBufferPointer(start: buf, count: length))
  79. }
  80. /**
  81. ISO/IEC 9797-1 Padding method 2.
  82. Add a single bit with value 1 to the end of the data.
  83. If necessary add bits with value 0 to the end of the data until the padded data is a multiple of blockSize.
  84. - parameters:
  85. - blockSize: Padding size in bytes.
  86. - allowance: Excluded trailing number of bytes.
  87. */
  88. @inline(__always) @inlinable
  89. func bitPadding(to data: inout Array<UInt8>, blockSize: Int, allowance: Int = 0) {
  90. let msgLength = data.count
  91. // Step 1. Append Padding Bits
  92. // append one bit (UInt8 with one bit) to message
  93. data.append(0x80)
  94. // Step 2. append "0" bit until message length in bits ≡ 448 (mod 512)
  95. let max = blockSize - allowance // 448, 986
  96. if msgLength % blockSize < max { // 448
  97. data += Array<UInt8>(repeating: 0, count: max - 1 - (msgLength % blockSize))
  98. } else {
  99. data += Array<UInt8>(repeating: 0, count: blockSize + max - 1 - (msgLength % blockSize))
  100. }
  101. }