Skip to content

Commit 2c75fde

Browse files
committed
[Algorithms] Support HS384 and HS512
1 parent aaa0cc4 commit 2c75fde

File tree

5 files changed

+44
-4
lines changed

5 files changed

+44
-4
lines changed

JWT/JWT.swift

Lines changed: 25 additions & 0 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,6 +42,10 @@ 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

@@ -45,6 +59,17 @@ public enum Algorithm : Printable {
4559
let mac = Authenticator.HMAC(key: key.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)!, variant:.sha256)
4660
let result = mac.authenticate(message.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)!)!
4761
return base64encode(result)
62+
63+
case .HS384(let key):
64+
let mac = Authenticator.HMAC(key: key.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)!, variant:.sha384)
65+
let result = mac.authenticate(message.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)!)!
66+
return base64encode(result)
67+
68+
case .HS512(let key):
69+
let mac = Authenticator.HMAC(key: key.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)!, variant:.sha512)
70+
let result = mac.authenticate(message.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)!)!
71+
return base64encode(result)
72+
4873
}
4974
}
5075

JWTTests/JWTTests.swift

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

Podfile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ platform :osx, '10.9'
22
use_frameworks!
33

44
target 'JWT' do
5-
podspec
5+
pod 'CryptoSwift', :head
66
end
77

88
target 'JWTTests' do
9-
podspec
9+
pod 'CryptoSwift', :head
1010
end
1111

Podfile.lock

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
PODS:
2-
- CryptoSwift (0.0.8)
2+
- CryptoSwift (HEAD based on 0.0.8)
33

44
DEPENDENCIES:
5-
- CryptoSwift (~> 0.0.8)
5+
- CryptoSwift (HEAD)
66

77
SPEC CHECKSUMS:
88
CryptoSwift: 6d1b93af5b48e02e57366bfad28b00170af405ee

README.md

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

6565
- None - Unsecured JWTs
6666
- HS256 - HMAC using SHA-256 hash algorithm (default)
67+
- HS384 - HMAC using SHA-384 hash algorithm
68+
- HS512 - HMAC using SHA-512 hash algorithm
6769

6870
#### Additional Algorithms
6971

0 commit comments

Comments
 (0)