Skip to content

[Algorithms] Support HS384 and HS512 #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions JWT/JWT.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ public enum Algorithm : Printable {
/// HMAC using SHA-256 hash algorithm
case HS256(String)

/// HMAC using SHA-384 hash algorithm
case HS384(String)

/// HMAC using SHA-512 hash algorithm
case HS512(String)

static func algorithm(name:String, key:String?) -> Algorithm? {
if name == "none" {
if let key = key {
Expand All @@ -20,6 +26,10 @@ public enum Algorithm : Printable {
} else if let key = key {
if name == "HS256" {
return .HS256(key)
} else if name == "HS384" {
return .HS384(key)
} else if name == "HS512" {
return .HS512(key)
}
}

Expand All @@ -32,6 +42,10 @@ public enum Algorithm : Printable {
return "none"
case .HS256(let key):
return "HS256"
case .HS384(let key):
return "HS384"
case .HS512(let key):
return "HS512"
}
}

Expand All @@ -45,6 +59,17 @@ public enum Algorithm : Printable {
let mac = Authenticator.HMAC(key: key.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)!, variant:.sha256)
let result = mac.authenticate(message.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)!)!
return base64encode(result)

case .HS384(let key):
let mac = Authenticator.HMAC(key: key.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)!, variant:.sha384)
let result = mac.authenticate(message.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)!)!
return base64encode(result)

case .HS512(let key):
let mac = Authenticator.HMAC(key: key.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)!, variant:.sha512)
let result = mac.authenticate(message.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)!)!
return base64encode(result)

}
}

Expand Down
13 changes: 13 additions & 0 deletions JWTTests/JWTTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,19 @@ class JWTDecodeTests : XCTestCase {
func testMatchesAnyAlgorithm() {
let jwt = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.e30.2_8pWJfyPup0YwOXK7g9Dn0cF1E3pdn299t4hSeJy5w."
assertFailure(decode(jwt, [.HS256("anothersecret"), .HS256("secret")]))

func testHS384Algorithm() {
let jwt = "eyJhbGciOiJIUzM4NCIsInR5cCI6IkpXVCJ9.eyJzb21lIjoicGF5bG9hZCJ9.lddiriKLoo42qXduMhCTKZ5Lo3njXxOC92uXyvbLyYKzbq4CVVQOb3MpDwnI19u4"
assertSuccess(decode(jwt, .HS384("secret"))) { payload in
XCTAssertEqual(payload as NSDictionary, ["some": "payload"])
}
}

func testHS512Algorithm() {
let jwt = "eyJhbGciOiJIUzUxMiIsInR5cCI6IkpXVCJ9.eyJzb21lIjoicGF5bG9hZCJ9.WTzLzFO079PduJiFIyzrOah54YaM8qoxH9fLMQoQhKtw3_fMGjImIOokijDkXVbyfBqhMo2GCNu4w9v7UXvnpA"
assertSuccess(decode(jwt, .HS512("secret"))) { payload in
XCTAssertEqual(payload as NSDictionary, ["some": "payload"])
}
}
}

Expand Down
4 changes: 2 additions & 2 deletions Podfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ platform :osx, '10.9'
use_frameworks!

target 'JWT' do
podspec
pod 'CryptoSwift', :head
end

target 'JWTTests' do
podspec
pod 'CryptoSwift', :head
end

4 changes: 2 additions & 2 deletions Podfile.lock
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
PODS:
- CryptoSwift (0.0.8)
- CryptoSwift (HEAD based on 0.0.8)

DEPENDENCIES:
- CryptoSwift (~> 0.0.8)
- CryptoSwift (HEAD)

SPEC CHECKSUMS:
CryptoSwift: 6d1b93af5b48e02e57366bfad28b00170af405ee
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ This library supports the following algorithms:

- None - Unsecured JWTs
- HS256 - HMAC using SHA-256 hash algorithm (default)
- HS384 - HMAC using SHA-384 hash algorithm
- HS512 - HMAC using SHA-512 hash algorithm

#### Additional Algorithms

Expand Down