Skip to content

Commit

Permalink
adding a new Iterator
Browse files Browse the repository at this point in the history
  • Loading branch information
maschall committed Oct 2, 2024
1 parent 5ba6c63 commit d94f57a
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 16 deletions.
42 changes: 30 additions & 12 deletions Sources/_CryptoExtras/AES/Nonces.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,30 @@ import Foundation
// any edits of this file WILL be overwritten and thus discarded
// see section `gyb` in `README` for details.

fileprivate struct ByteIterator<T>: IteratorProtocol {
var currentOffset = 0
var pointer: UnsafeRawBufferPointer? = nil
let length: Int

init(_ bytes: T) {
self.length = Mirror(reflecting: bytes).children.count
withUnsafeBytes(of: bytes) { pointer in
self.pointer = pointer
}
}

@inlinable
public mutating func next() -> UInt8? {
guard let pointer,
currentOffset < length else { return nil }

let next = pointer.load(fromByteOffset: currentOffset, as: UInt8.self)
currentOffset += 1
return next
}
}


// MARK: - AES._CBC + IV
extension AES._CBC {
/// A value used once during a cryptographic operation and then discarded.
Expand Down Expand Up @@ -109,10 +133,8 @@ extension AES._CBC {
}

/// Returns an iterator over the elements of the nonce.
public func makeIterator() -> Array<UInt8>.Iterator {
self.withUnsafeBytes({ (buffPtr) in
return Array(buffPtr).makeIterator()
})
public func makeIterator() -> some IteratorProtocol<UInt8> {
ByteIterator(bytes)
}
}
}
Expand Down Expand Up @@ -202,10 +224,8 @@ extension AES._CFB {
}

/// Returns an iterator over the elements of the nonce.
public func makeIterator() -> Array<UInt8>.Iterator {
self.withUnsafeBytes({ (buffPtr) in
return Array(buffPtr).makeIterator()
})
public func makeIterator() -> some IteratorProtocol<UInt8> {
ByteIterator(bytes)
}
}
}
Expand Down Expand Up @@ -295,10 +315,8 @@ extension AES._CTR {
}

/// Returns an iterator over the elements of the nonce.
public func makeIterator() -> Array<UInt8>.Iterator {
self.withUnsafeBytes({ (buffPtr) in
return Array(buffPtr).makeIterator()
})
public func makeIterator() -> some IteratorProtocol<UInt8> {
ByteIterator(bytes)
}
}
}
30 changes: 26 additions & 4 deletions Sources/_CryptoExtras/AES/Nonces.swift.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,30 @@ import Foundation
// MARK: - Generated file, do NOT edit
// any edits of this file WILL be overwritten and thus discarded
// see section `gyb` in `README` for details.

fileprivate struct ByteIterator<T>: IteratorProtocol {
var currentOffset = 0
var pointer: UnsafeRawBufferPointer? = nil
let length: Int

init(_ bytes: T) {
self.length = Mirror(reflecting: bytes).children.count
withUnsafeBytes(of: bytes) { pointer in
self.pointer = pointer
}
}

@inlinable
public mutating func next() -> UInt8? {
guard let pointer,
currentOffset < length else { return nil }

let next = pointer.load(fromByteOffset: currentOffset, as: UInt8.self)
currentOffset += 1
return next
}
}

%{
ciphers = [
{"name": "AES._CBC", "nonceName": "IV", "tupleDefinition": """(
Expand Down Expand Up @@ -123,10 +147,8 @@ extension ${name} {
}

/// Returns an iterator over the elements of the nonce.
public func makeIterator() -> Array<UInt8>.Iterator {
self.withUnsafeBytes({ (buffPtr) in
return Array(buffPtr).makeIterator()
})
public func makeIterator() -> some IteratorProtocol<UInt8> {
ByteIterator(bytes)
}
}
}
Expand Down

0 comments on commit d94f57a

Please sign in to comment.