Jian Li | f8c2d4a | 2016-09-15 02:33:12 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016-present Open Networking Laboratory |
| 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 org.onosproject.lisp.msg.authentication; |
| 17 | |
| 18 | /** |
| 19 | * A factory class that returns LISP authentication instance. |
| 20 | */ |
| 21 | public final class LispAuthenticationFactory { |
| 22 | |
| 23 | /** |
Jian Li | 5b2b236 | 2016-11-27 17:38:19 +0900 | [diff] [blame] | 24 | * Prevents object instantiation from external. |
| 25 | */ |
| 26 | private LispAuthenticationFactory() { |
| 27 | } |
| 28 | |
| 29 | /** |
Jian Li | f8c2d4a | 2016-09-15 02:33:12 +0900 | [diff] [blame] | 30 | * Obtains a factory singleton instance. |
| 31 | * |
| 32 | * @return factory singleton instance |
| 33 | */ |
| 34 | public static LispAuthenticationFactory getInstance() { |
| 35 | return SingletonHelper.INSTANCE; |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * Generates a new authentication data with given authentication key and |
| 40 | * authentication type. |
| 41 | * |
| 42 | * @param authType authentication key type |
| 43 | * @param authKey authentication key string |
Jian Li | afe2d3f | 2016-11-01 02:49:07 +0900 | [diff] [blame] | 44 | * @param data authentication data |
Jian Li | f8c2d4a | 2016-09-15 02:33:12 +0900 | [diff] [blame] | 45 | * @return authentication data |
| 46 | */ |
| 47 | public byte[] createAuthenticationData(LispAuthenticationKeyEnum authType, |
Jian Li | afe2d3f | 2016-11-01 02:49:07 +0900 | [diff] [blame] | 48 | String authKey, byte[] data) { |
Jian Li | f8c2d4a | 2016-09-15 02:33:12 +0900 | [diff] [blame] | 49 | LispMacAuthentication macAuth = new LispMacAuthentication(authType); |
Jian Li | f8c2d4a | 2016-09-15 02:33:12 +0900 | [diff] [blame] | 50 | byte[] authData; |
| 51 | switch (authType) { |
| 52 | case SHA1: |
| 53 | case SHA256: |
Jian Li | afe2d3f | 2016-11-01 02:49:07 +0900 | [diff] [blame] | 54 | authData = macAuth.getAuthenticationData(authKey, data); |
Jian Li | f8c2d4a | 2016-09-15 02:33:12 +0900 | [diff] [blame] | 55 | break; |
| 56 | case NONE: |
| 57 | case UNKNOWN: |
| 58 | default: |
| 59 | authData = macAuth.getAuthenticationData(); |
| 60 | break; |
| 61 | } |
| 62 | return authData; |
| 63 | } |
| 64 | |
| 65 | /** |
Jian Li | 5b2b236 | 2016-11-27 17:38:19 +0900 | [diff] [blame] | 66 | * A private singleton helper class. |
Jian Li | f8c2d4a | 2016-09-15 02:33:12 +0900 | [diff] [blame] | 67 | */ |
Jian Li | 5b2b236 | 2016-11-27 17:38:19 +0900 | [diff] [blame] | 68 | private static final class SingletonHelper { |
Jian Li | f8c2d4a | 2016-09-15 02:33:12 +0900 | [diff] [blame] | 69 | private static final LispAuthenticationFactory INSTANCE = |
Jian Li | 5b2b236 | 2016-11-27 17:38:19 +0900 | [diff] [blame] | 70 | new LispAuthenticationFactory(); |
| 71 | private static final String ILLEGAL_ACCESS_MSG = "Should not instantiate this class."; |
| 72 | |
| 73 | private SingletonHelper() { |
| 74 | throw new IllegalAccessError(ILLEGAL_ACCESS_MSG); |
| 75 | } |
Jian Li | f8c2d4a | 2016-09-15 02:33:12 +0900 | [diff] [blame] | 76 | } |
| 77 | } |