Skip to content

Commit 7813bd2

Browse files
committed
Support HS384 and HS512
Closes #4
1 parent c46268f commit 7813bd2

File tree

3 files changed

+45
-14
lines changed

3 files changed

+45
-14
lines changed

JWT/JWT.swift

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@ public enum Algorithm : Printable {
1111
/// HMAC using SHA-256 hash algorithm
1212
case HS256(String)
1313

14+
/// HMAC using SHA-384 hash algorithm
15+
case HS384(String)
16+
17+
/// HMAC using SHA-512 hash algorithm
18+
case HS512(String)
19+
1420
static func algorithm(name:String, key:String?) -> Algorithm? {
1521
if name == "none" {
1622
if let key = key {
@@ -20,6 +26,10 @@ public enum Algorithm : Printable {
2026
} else if let key = key {
2127
if name == "HS256" {
2228
return .HS256(key)
29+
} else if name == "HS384" {
30+
return .HS384(key)
31+
} else if name == "HS512" {
32+
return .HS512(key)
2333
}
2434
}
2535

@@ -32,21 +42,35 @@ public enum Algorithm : Printable {
3242
return "none"
3343
case .HS256(let key):
3444
return "HS256"
45+
case .HS384(let key):
46+
return "HS384"
47+
case .HS512(let key):
48+
return "HS512"
3549
}
3650
}
3751

3852
/// Sign a message using the algorithm
3953
func sign(message:String) -> String {
54+
func signHS(key:String, variant:CryptoSwift.HMAC.Variant) -> String {
55+
let keyData = key.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)!
56+
let messageData = message.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)!
57+
let mac = Authenticator.HMAC(key: keyData.arrayOfBytes(), variant:variant)
58+
let result = mac.authenticate(messageData.arrayOfBytes())!
59+
return base64encode(NSData.withBytes(result))
60+
}
61+
4062
switch self {
4163
case .None:
4264
return ""
4365

4466
case .HS256(let key):
45-
let keyData = key.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)!
46-
let messageData = message.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)!
47-
let mac = Authenticator.HMAC(key: keyData.arrayOfBytes(), variant:.sha256)
48-
let result = mac.authenticate(messageData.arrayOfBytes())!
49-
return base64encode(NSData.withBytes(result))
67+
return signHS(key, .sha256)
68+
69+
case .HS384(let key):
70+
return signHS(key, .sha384)
71+
72+
case .HS512(let key):
73+
return signHS(key, .sha512)
5074
}
5175
}
5276

JWTTests/JWTTests.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,20 @@ class JWTDecodeTests : XCTestCase {
217217
let jwt = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.e30.2_8pWJfyPup0YwOXK7g9Dn0cF1E3pdn299t4hSeJy5w."
218218
assertFailure(decode(jwt, [.HS256("anothersecret"), .HS256("secret")]))
219219
}
220+
221+
func testHS384Algorithm() {
222+
let jwt = "eyJhbGciOiJIUzM4NCIsInR5cCI6IkpXVCJ9.eyJzb21lIjoicGF5bG9hZCJ9.lddiriKLoo42qXduMhCTKZ5Lo3njXxOC92uXyvbLyYKzbq4CVVQOb3MpDwnI19u4"
223+
assertSuccess(decode(jwt, .HS384("secret"))) { payload in
224+
XCTAssertEqual(payload as NSDictionary, ["some": "payload"])
225+
}
226+
}
227+
228+
func testHS512Algorithm() {
229+
let jwt = "eyJhbGciOiJIUzUxMiIsInR5cCI6IkpXVCJ9.eyJzb21lIjoicGF5bG9hZCJ9.WTzLzFO079PduJiFIyzrOah54YaM8qoxH9fLMQoQhKtw3_fMGjImIOokijDkXVbyfBqhMo2GCNu4w9v7UXvnpA"
230+
assertSuccess(decode(jwt, .HS512("secret"))) { payload in
231+
XCTAssertEqual(payload as NSDictionary, ["some": "payload"])
232+
}
233+
}
220234
}
221235

222236
// MARK: Helpers

README.md

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -64,15 +64,8 @@ This library supports the following algorithms:
6464

6565
- None - Unsecured JWTs
6666
- HS256 - HMAC using SHA-256 hash algorithm (default)
67-
68-
#### Additional Algorithms
69-
70-
Support for HS384 and HS512 can be found in the `algorithms-hs` branch which depends on an unreleased version of CryptoSwift. It can be installed via:
71-
72-
```ruby
73-
pod 'JSONWebToken', :git => 'https://github.com/kylef/JSONWebToken.swift.git', :branch => 'algorithms-hs'
74-
pod 'CryptoSwift', :head
75-
```
67+
- HS384 - HMAC using SHA-384 hash algorithm
68+
- HS512 - HMAC using SHA-384 hash algorithm
7669

7770
## License
7871

0 commit comments

Comments
 (0)