Skip to content

Commit 4b0617e

Browse files
committed
[Swift 3.2] Updated playgrounds.
1 parent 9432c71 commit 4b0617e

36 files changed

+411
-406
lines changed
Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
2+
import Foundation
3+
//
4+
// If you get an error on the line below you need to run:
5+
// sudo xcrun -sdk macosx swift GenerateCommonCryptoModule.swift macosx
6+
//
7+
//import CommonCrypto
8+
import IDZSwiftCommonCrypto
9+
10+
11+
// MARK: - Message Digest Demo
12+
let s = "The quick brown fox jumps over the lazy dog."
13+
var md5 = Digest(algorithm: .md5)
14+
md5.update(string: s)
15+
var digest = md5.final()
16+
var md5String = hexString(fromArray: digest)
17+
18+
s.MD5
19+
20+
// MARK: - HMAC Demogit s
21+
// Data from RFC 2202
22+
var key = arrayFrom(hexString: "0102030405060708090a0b0c0d0e0f10111213141516171819")
23+
var data : [UInt8] = Array(repeating:0xcd, count:50)
24+
var expected = arrayFrom(hexString: "4c9007f4026250c6bc8414f9bf50c86c2d7235da")
25+
var hmac = HMAC(algorithm:.sha1, key:key).update(byteArray: data)?.final()
26+
var sha1String = hexString(fromArray: hmac!)
27+
sha1String
28+
29+
30+
// MARK: - Key Digest Demo
31+
// Data from RFC 6070
32+
33+
34+
let tests = [ ("password", "salt", 1, 20, "0c60c80f961f0e71f3a9b524af6012062fe037a6")]
35+
for (password, salt, rounds, dkLen, expected) in tests
36+
{
37+
let key0 = PBKDF.deriveKey(password: password, salt: salt, prf: .sha1, rounds: uint(rounds), derivedKeyLength: UInt(dkLen))
38+
let keyString0 = hexString(fromArray: key0)
39+
let key1 = PBKDF.deriveKey(password: password, salt: arrayFrom(string: salt), prf: .sha1, rounds: uint(rounds), derivedKeyLength: UInt(dkLen))
40+
let keyString1 = hexString(fromArray: key1)
41+
}
42+
43+
// MARK: - Random Demo
44+
var randomBytes = hexString(fromArray: try Random.generateBytes(byteCount: 16))
45+
46+
do {
47+
try Random.generateBytesThrow(byteCount: 16)
48+
}
49+
catch let e {
50+
print("generateBytesThrow threw \(e)")
51+
}
52+
53+
54+
do {
55+
try Random.generateBytesThrow(byteCount: 16)
56+
}
57+
catch {
58+
print("generateBytesThrow threw an error (expected).")
59+
}
60+
61+
62+
// MARK: - Crypto Demo
63+
// Test data from NIST Special Publication
64+
// F.1.1 p24
65+
// http://csrc.nist.gov/publications/nistpubs/800-38a/sp800-38a.pdf
66+
func test_StreamCryptor_AES_ECB() {
67+
let key = arrayFrom(hexString: "2b7e151628aed2a6abf7158809cf4f3c")
68+
let plainText = arrayFrom(hexString: "6bc1bee22e409f96e93d7e117393172a")
69+
let expectedCipherText = arrayFrom(hexString:"3ad77bb40d7a3660a89ecaf32466ef97")
70+
71+
let aesEncrypt = StreamCryptor(operation:.encrypt, algorithm:.aes, options:.ECBMode, key:key, iv:Array<UInt8>())
72+
var cipherText : [UInt8] = []
73+
var dataOut = Array<UInt8>(repeating:UInt8(0), count:plainText.count)
74+
let (byteCount, status) = aesEncrypt.update(byteArrayIn: plainText, byteArrayOut: &dataOut)
75+
dataOut
76+
"\(status)"
77+
status
78+
79+
cipherText += dataOut[0..<Int(byteCount)]
80+
//(byteCount, status) = aesEncrypt.final(&dataOut)
81+
//assert(byteCount == 0, "Final byte count is 0")
82+
assert(expectedCipherText.count == cipherText.count , "Counts are as expected")
83+
assert(expectedCipherText == cipherText, "Obtained expected cipher text")
84+
}
85+
86+
test_StreamCryptor_AES_ECB()
87+
// Single block ECB mode
88+
func test_Cryptor_AES_ECB_1() {
89+
let key = arrayFrom(hexString: "2b7e151628aed2a6abf7158809cf4f3c")
90+
let plainText = arrayFrom(hexString: "6bc1bee22e409f96e93d7e117393172a")
91+
let expectedCipherText = arrayFrom(hexString: "3ad77bb40d7a3660a89ecaf32466ef97")
92+
93+
let cipherText = Cryptor(operation:.encrypt, algorithm:.aes, options:.ECBMode, key:key, iv:Array<UInt8>()).update(byteArray: plainText)?.final()
94+
95+
assert(expectedCipherText.count == cipherText!.count , "Counts are as expected")
96+
assert(expectedCipherText == cipherText!, "Obtained expected cipher text")
97+
}
98+
99+
test_Cryptor_AES_ECB_1()
100+
101+
// Double repeated block ECB mode
102+
// Shows weakness of ECB mode -- same plaintext block gets same ciphertext
103+
func test_Cryptor_AES_ECB_2() {
104+
let key = arrayFrom(hexString: "2b7e151628aed2a6abf7158809cf4f3c")
105+
var plainText = arrayFrom(hexString: "6bc1bee22e409f96e93d7e117393172a")
106+
var expectedCipherText = arrayFrom(hexString: "3ad77bb40d7a3660a89ecaf32466ef97")
107+
108+
plainText += plainText
109+
expectedCipherText += expectedCipherText
110+
111+
let cipherText = Cryptor(operation:.encrypt, algorithm:.aes, options:.ECBMode, key:key, iv:Array<UInt8>()).update(byteArray: plainText)?.final()
112+
113+
assert(expectedCipherText.count == cipherText!.count , "Counts are as expected")
114+
assert(expectedCipherText == cipherText!, "Obtained expected cipher text")
115+
}
116+
117+
test_Cryptor_AES_ECB_2()
118+
119+
120+
121+
122+
// Single block ECB mode
123+
func test_Cryptor_AES_ECB_Short() {
124+
let key = arrayFrom(hexString:"2b7e151628aed2a6abf7158809cf4f3c")
125+
let plainText = arrayFrom(hexString: "6bc1bee22e409f96e93d7e11739317")
126+
let expectedCipherText = arrayFrom(hexString: "3ad77bb40d7a3660a89ecaf32466ef97")
127+
128+
let cryptor = Cryptor(operation:.encrypt, algorithm:.aes, options:.ECBMode, key:key, iv:Array<UInt8>())
129+
let cipherText = cryptor.update(byteArray: plainText)?.final()
130+
if(cipherText == nil)
131+
{
132+
print("Encryption failed (as expected) with status \(cryptor.status)")
133+
}
134+
}
135+
136+
test_Cryptor_AES_ECB_Short()
137+
138+
func test_Cryptor_AES_ECB_Short_Padding() {
139+
let key = arrayFrom(hexString: "2b7e151628aed2a6abf7158809cf4f3c")
140+
let plainText = arrayFrom(hexString: "6bc1bee22e409f96e93d7e11739317")
141+
let expectedCipherText = arrayFrom(hexString: "21ea2ba3e445a0ef710a7c26618d1975")
142+
143+
let cryptor = Cryptor(operation:.encrypt,
144+
algorithm:.aes,
145+
options:[.ECBMode,.PKCS7Padding], key:key, iv:Array<UInt8>())
146+
let cipherText = cryptor.update(byteArray: plainText)?.final()
147+
assert(cipherText != nil)
148+
assert(cipherText! == expectedCipherText)
149+
}
150+
151+
//test_Cryptor_AES_ECB_Short_Padding()
152+
153+
154+
//test_Cryptor_AES_CBC_1()
155+
156+
func test_Cryptor_AES_CBC_2() {
157+
let key = arrayFrom(hexString: "2b7e151628aed2a6abf7158809cf4f3c")
158+
var plainText = arrayFrom(hexString: "6bc1bee22e409f96e93d7e117393172a")
159+
let expectedCipherText = arrayFrom(hexString: "3ad77bb40d7a3660a89ecaf32466ef97025c61efee87e604cd1b12ce9dde5c51")
160+
161+
plainText += plainText
162+
163+
let optionalCipherText = Cryptor(operation:.encrypt, algorithm:.aes, options:.None, key:key, iv:Array<UInt8>()).update(byteArray: plainText)?.final()
164+
if let cipherText = optionalCipherText
165+
{
166+
167+
168+
assert(expectedCipherText.count == cipherText.count , "Counts are as expected")
169+
assert(expectedCipherText == cipherText, "Obtained expected cipher text")
170+
}
171+
}
172+
173+
174+
175+
176+
177+
178+
Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,2 @@
11
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2-
<playground version='3.0' sdk='macosx'>
3-
<sections>
4-
<code source-file-name='section-1.swift'/>
5-
</sections>
6-
<timeline fileName='timeline.xctimeline'/>
7-
</playground>
2+
<playground version='6.0' target-platform='macos' display-mode='raw'/>

DemoPlayground.playground/section-1.swift

Lines changed: 0 additions & 177 deletions
This file was deleted.

DemoPlayground.playground/timeline.xctimeline

Lines changed: 0 additions & 6 deletions
This file was deleted.

0 commit comments

Comments
 (0)