Browse Source

Byte init with [Bit]

Marcin Krzyżanowski 11 years ago
parent
commit
d0d13d3e14
2 changed files with 19 additions and 7 deletions
  1. 18 6
      CryptoSwift/ByteExtension.swift
  2. 1 1
      CryptoSwift/IntExtension.swift

+ 18 - 6
CryptoSwift/ByteExtension.swift

@@ -9,15 +9,27 @@
 import Foundation
 
 extension Byte {
+
+    init(bits: [Bit]) {
+        var bitPattern:Byte = 0
+        for (idx,b) in enumerate(bits) {
+            if (b == Bit.Zero) {
+                var bit:Byte = Byte(1) << Byte(idx)
+                bitPattern = bitPattern | bit
+            }
+        }
+        
+        self.init(bitPattern)
+    }
     
     /** array of bits */
-    internal func bits() -> [Bit] {
-        let totalBitsCount = sizeof(Byte) * 8
+    func bits() -> [Bit] {
+        let totalBitsCount = sizeofValue(self) * 8
         
-        var bitsArray:[Bit] = [Bit](count: totalBitsCount, repeatedValue: Bit.Zero)
+        var bitsArray = [Bit](count: totalBitsCount, repeatedValue: Bit.Zero)
         
         for j in 0..<totalBitsCount {
-            let bitVal:Byte = 1 << UInt8(totalBitsCount - 1 - j)
+            let bitVal:Byte = 1 << Byte(totalBitsCount - 1 - j)
             let check = self & bitVal
             
             if (check != 0) {
@@ -26,8 +38,8 @@ extension Byte {
         }
         return bitsArray
     }
-    
-    internal func bits() -> String {
+
+    func bits() -> String {
         var s = String()
         let arr:[Bit] = self.bits()
         for (idx,b) in enumerate(arr) {

+ 1 - 1
CryptoSwift/IntExtension.swift

@@ -83,7 +83,7 @@ extension UInt64 {
 
 extension Int {
     
-    private init(bits: [Bit]) {
+    init(bits: [Bit]) {
         var bitPattern:UInt = 0
         for (idx,b) in enumerate(bits) {
             if (b == Bit.Zero) {