Skip to content

Commit

Permalink
Merge pull request #172 from qutheory/cryptokitten-update
Browse files Browse the repository at this point in the history
update to live cryptokitten
  • Loading branch information
tanner0101 committed Apr 7, 2016
2 parents 693348e + d64ddd3 commit 809a0da
Show file tree
Hide file tree
Showing 10 changed files with 430 additions and 384 deletions.
1 change: 0 additions & 1 deletion .hound.yml
@@ -1,3 +1,2 @@
swift:
config_file: .swiftlint.yml
fail_on_violations: true
4 changes: 3 additions & 1 deletion .swiftlint.yml
@@ -1,6 +1,8 @@
disabled_rules:
opt_in_rules:
- missing_docs
included:
excluded:
- Packages
- Sources/Vapor/Core/Generated.swift
- Sources/Generator
- Sources/Generator
14 changes: 12 additions & 2 deletions Package.swift
Expand Up @@ -3,11 +3,21 @@ import PackageDescription
let package = Package(
name: "Vapor",
dependencies: [
//Standards package. Contains protocols for cross-project compatability.
.Package(url: "https://github.com/swiftx/s4.git", majorVersion: 0, minor: 2),

//Provides critical String functions Foundation is missing on Linux
.Package(url: "https://github.com/Zewo/String.git", majorVersion: 0, minor: 4),

//Parses and serializes JSON
.Package(url: "https://github.com/Zewo/JSON.git", majorVersion: 0, minor: 4),

//Swift wrapper around Sockets, used for built-in HTTP server
.Package(url: "https://github.com/ketzusaka/Hummingbird.git", majorVersion: 1, minor: 1),
.Package(url: "https://github.com/swiftx/s4.git", majorVersion: 0, minor: 2),
.Package(url: "https://github.com/qutheory/HMAC.git", majorVersion: 0, minor: 2),

//SHA2 + HMAC hashing. Used by the core to create session identifiers.
.Package(url: "https://github.com/CryptoKitten/HMAC.git", majorVersion: 0, minor: 3),
.Package(url: "https://github.com/CryptoKitten/SHA2.git", majorVersion: 0, minor: 1)
],
exclude: [
"XcodeProject",
Expand Down
2 changes: 1 addition & 1 deletion Sources/Vapor/Core/Application.swift
@@ -1,7 +1,7 @@
import libc

public class Application {
public static let VERSION = "0.5.0"
public static let VERSION = "0.5.1"

/**
The router driver is responsible
Expand Down
2 changes: 1 addition & 1 deletion Sources/Vapor/Hash/Hash.swift
Expand Up @@ -21,7 +21,7 @@ public class Hash {
It will be used to create the hashes
request by functions like `make()`
*/
public var driver: HashDriver = SHAHasher()
public var driver: HashDriver = SHA2Hasher(variant: .sha256)

/**
Hashes a string using the `Hash` class's
Expand Down
36 changes: 27 additions & 9 deletions Sources/Vapor/Hash/SHAHasher.swift
@@ -1,22 +1,40 @@
import HMAC
import SHA1
import SHA2

/**
Create SHA1 + HMAC hashes with the
Create SHA + HMAC hashes with the
Hash class by applying this driver.
*/
public class SHAHasher: HashDriver {
public class SHA2Hasher: HashDriver {

var variant: Variant

init(variant: Variant) {
self.variant = variant
}

public enum Variant {
case sha256
case sha384
case sha512
}

public func hash(message: String, key: String) -> String {
let keyBuff = key.data.bytes
let msgBuff = message.data.bytes

var msgBuff = [UInt8]()
msgBuff += message.utf8
let hashed: [Byte]

var keyBuff = [UInt8]()
keyBuff += key.utf8
switch variant {
case .sha256:
hashed = HMAC<SHA2<SHA256>>.authenticate(msgBuff, withKey: keyBuff)
case .sha384:
hashed = HMAC<SHA2<SHA384>>.authenticate(msgBuff, withKey: keyBuff)
case .sha512:
hashed = HMAC<SHA2<SHA512>>.authenticate(msgBuff, withKey: keyBuff)
}

let hmac = HMAC.authenticate(key: keyBuff, message: msgBuff, variant: SHA1.self)
return hmac.toHexString()
return hashed.toHexString()
}

}
27 changes: 20 additions & 7 deletions Tests/Vapor/HashTests.swift
Expand Up @@ -21,18 +21,31 @@ class HashTests: XCTestCase {
let app = Application()

let string = "vapor"
let expected = "01c99e8ec3c38b91c1c7a7add05044438d9aaa16"
app.hash.key = "123"
let defaultExpected = "97ce9a45eaf0b1ceafc3bba00dfec047526386bbd69241e4a4f0c9fde7c638ea"
let defaultKey = "123"

//test app facade
app.hash.key = defaultKey
let result = app.hash.make(string)
XCTAssert(defaultExpected == result, "Hash did not match")

XCTAssert(expected == result, "Hash did not match")

//change key
app.hash.key = "1234"

let badResult = app.hash.make(string)

XCTAssert(expected != badResult, "Hash matched bad result")
XCTAssert(defaultExpected != badResult, "Hash matched bad result")

//test all variants manually
var expected: [SHA2Hasher.Variant: String] = [:]
expected[.sha256] = "97ce9a45eaf0b1ceafc3bba00dfec047526386bbd69241e4a4f0c9fde7c638ea"
expected[.sha384] = "3977579292ed6c50588c5e2e345e84470a8e7f2635ecd89cacedb9d747d05bddb767c2c6943f7ed8ae3abf8c8000bd89"
expected[.sha512] = "9215c98b5ea5826961395de57f8e4cd2baf3d08c429d4db0f4e2d83feb12e989ffbc7dbf8611ed65ef13e6e8d5f370a803065708f38fd73a349f0869b7891bc6"

for (variant, expect) in expected {
let hasher = SHA2Hasher(variant: variant)
let result = hasher.hash(string, key: defaultKey)
XCTAssert(result == expect, "Hash for \(variant) did not match")
}

}

}

0 comments on commit 809a0da

Please sign in to comment.