1 /*
2  * Copyright (C) 2014 jsonwebtoken.io
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package io.jsonwebtoken.impl.crypto;
17
18 import io.jsonwebtoken.SignatureAlgorithm;
19 import io.jsonwebtoken.lang.Assert;
20
21 import java.security.Key;
22
23 public class DefaultSignerFactory implements SignerFactory {
24
25     public static final SignerFactory INSTANCE = new DefaultSignerFactory();
26
27     @Override
28     public Signer createSigner(SignatureAlgorithm alg, Key key) {
29         Assert.notNull(alg, "SignatureAlgorithm cannot be null.");
30         Assert.notNull(key, "Signing Key cannot be null.");
31
32         switch (alg) {
33             case HS256:
34             case HS384:
35             case HS512:
36                 return new MacSigner(alg, key);
37             case RS256:
38             case RS384:
39             case RS512:
40             case PS256:
41             case PS384:
42             case PS512:
43                 return new RsaSigner(alg, key);
44             case ES256:
45             case ES384:
46             case ES512:
47                 return new EllipticCurveSigner(alg, key);
48             default:
49                 throw new IllegalArgumentException("The '" + alg.name() + "' algorithm cannot be used for signing.");
50         }
51     }
52 }
53