Skip to content

Commit 2e87bc7

Browse files
committed
[Algorithms] Support HS384 and HS512
1 parent 99c3624 commit 2e87bc7

File tree

5 files changed

+45
-4
lines changed

5 files changed

+45
-4
lines changed

JWT/JWT.swift

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,22 @@ 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
return Algorithm.None
1723
} else if let key = key {
1824
if name == "HS256" {
1925
return .HS256(key)
26+
} else if name == "HS384" {
27+
return .HS384(key)
28+
} else if name == "HS512" {
29+
return .HS512(key)
2030
}
2131
}
2232

@@ -29,6 +39,10 @@ public enum Algorithm : Printable {
2939
return "none"
3040
case .HS256(let key):
3141
return "HS256"
42+
case .HS384(let key):
43+
return "HS384"
44+
case .HS512(let key):
45+
return "HS512"
3246
}
3347
}
3448

@@ -42,6 +56,17 @@ public enum Algorithm : Printable {
4256
let mac = Authenticator.HMAC(key: key.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)!, variant:.sha256)
4357
let result = mac.authenticate(message.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)!)!
4458
return base64encode(result)
59+
60+
case .HS384(let key):
61+
let mac = Authenticator.HMAC(key: key.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)!, variant:.sha384)
62+
let result = mac.authenticate(message.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)!)!
63+
return base64encode(result)
64+
65+
case .HS512(let key):
66+
let mac = Authenticator.HMAC(key: key.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)!, variant:.sha512)
67+
let result = mac.authenticate(message.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)!)!
68+
return base64encode(result)
69+
4570
}
4671
}
4772

JWTTests/JWTTests.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,20 @@ class JWTDecodeTests : XCTestCase {
143143
XCTAssertEqual(payload as NSDictionary, ["test": "ing"])
144144
}
145145
}
146+
147+
func testHS384Algorithm() {
148+
let jwt = "eyJhbGciOiJIUzM4NCIsInR5cCI6IkpXVCJ9.eyJzb21lIjoicGF5bG9hZCJ9.lddiriKLoo42qXduMhCTKZ5Lo3njXxOC92uXyvbLyYKzbq4CVVQOb3MpDwnI19u4"
149+
assertSuccess(decode(jwt, key:"secret")) { payload in
150+
XCTAssertEqual(payload as NSDictionary, ["some": "payload"])
151+
}
152+
}
153+
154+
func testHS512Algorithm() {
155+
let jwt = "eyJhbGciOiJIUzUxMiIsInR5cCI6IkpXVCJ9.eyJzb21lIjoicGF5bG9hZCJ9.WTzLzFO079PduJiFIyzrOah54YaM8qoxH9fLMQoQhKtw3_fMGjImIOokijDkXVbyfBqhMo2GCNu4w9v7UXvnpA"
156+
assertSuccess(decode(jwt, key:"secret")) { payload in
157+
XCTAssertEqual(payload as NSDictionary, ["some": "payload"])
158+
}
159+
}
146160
}
147161

148162
// MARK: Helpers

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
@@ -46,6 +46,8 @@ This library supports the following algorithms:
4646

4747
- None - Unsecured JWTs
4848
- HS256 - HMAC using SHA-256 hash algorithm (default)
49+
- HS384 - HMAC using SHA-384 hash algorithm
50+
- HS512 - HMAC using SHA-512 hash algorithm
4951

5052
## License
5153

0 commit comments

Comments
 (0)