소스 검색

SecureBytes conforms to Collection

Marcin Krzyżanowski 8 년 전
부모
커밋
7153db0d48
1개의 변경된 파일27개의 추가작업 그리고 3개의 파일을 삭제
  1. 27 3
      Sources/CryptoSwift/SecureBytes.swift

+ 27 - 3
Sources/CryptoSwift/SecureBytes.swift

@@ -15,7 +15,7 @@
 ///  Keeps bytes in memory. Because this is class, bytes are not copied
 ///  and memory area is locked as long as referenced, then unlocked on deinit
 final class SecureBytes {
-    private let bytes: Array<UInt8>
+    fileprivate let bytes: Array<UInt8>
     let count: Int
 
     init(bytes: Array<UInt8>) {
@@ -31,8 +31,32 @@ final class SecureBytes {
             munlock(pointer.baseAddress, pointer.count)
         }
     }
+}
+
+extension SecureBytes: Collection {
+    typealias Index = Int
+
+    var endIndex: Int {
+        return self.bytes.endIndex
+    }
+
+    var startIndex: Int {
+        return self.bytes.startIndex
+    }
+
+    subscript(position: Index) -> UInt8 {
+        return self.bytes[position]
+    }
+
+    subscript(bounds: Range<Index>) -> ArraySlice<UInt8> {
+        return self.bytes[bounds]
+    }
+
+    func formIndex(after i: inout Int) {
+        self.bytes.formIndex(after: &i)
+    }
 
-    subscript(index: Int) -> UInt8 {
-        return self.bytes[index]
+    func index(after i: Int) -> Int {
+        return self.bytes.index(after: i)
     }
 }