CommonLibrary(util/rest-client) code upload.
[vfc/nfvo/wfengine.git] / CommonLibrary / common-util / src / main / java / org / openo / baseservice / encrypt / cbb / sha / Sha256.java
1 /*
2  * Copyright (c) 2016, Huawei Technologies Co., Ltd.
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.openo.baseservice.encrypt.cbb.sha;
17
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
20
21 import java.nio.charset.StandardCharsets;
22 import java.security.InvalidKeyException;
23 import java.security.Key;
24 import java.security.MessageDigest;
25 import java.security.NoSuchAlgorithmException;
26
27 import javax.crypto.Mac;
28 import javax.crypto.spec.SecretKeySpec;
29 import javax.xml.bind.DatatypeConverter;
30
31 /**
32  * Utility to generate SHA256 digest and HMAC.<br/>
33  * <p>
34  * </p>
35  * 
36  * @author
37  * @version SDNO 0.5 03-Jun-2016
38  */
39 public final class Sha256 {
40
41     private static final Logger LOGGER = LoggerFactory.getLogger(Sha256.class);
42
43     private Sha256() {
44
45     }
46
47     /**
48      * Generates SHA256 digest.<br/>
49      * 
50      * @param data: The data to be digested.
51      * @return Hex encoded digested data.
52      * @since SDNO 0.5
53      */
54     public static String digest(final String data) {
55         final byte[] dataBytes = data.getBytes(StandardCharsets.UTF_8);
56         MessageDigest md = null;
57         try {
58             md = MessageDigest.getInstance("SHA-256");
59         } catch(final NoSuchAlgorithmException e) {
60             LOGGER.error("No SHA-256 support ", e);
61             return "";
62         }
63         final byte[] digest = md.digest(dataBytes);
64         return DatatypeConverter.printHexBinary(digest);
65     }
66
67     /**
68      * Generates hmac signature using data and key.<br/>
69      * 
70      * @param data: The data to be signed.
71      * @param key: The signing key.
72      * @return Hex encoded HMAC signature.
73      * @throws InvalidKeyException if the key is invalid.
74      * @since SDNO 0.5
75      */
76     public static String mac(final String data, final Key key) throws InvalidKeyException {
77         final byte[] dataBytes = data.getBytes(StandardCharsets.UTF_8);
78         Mac mac = null;
79         try {
80             mac = Mac.getInstance("HmacSHA256");
81             mac.init(key);
82         } catch(final NoSuchAlgorithmException e) {
83             LOGGER.error("SHA mac not supported", e);
84             return "";
85         }
86         final byte[] digest = mac.doFinal(dataBytes);
87         return DatatypeConverter.printHexBinary(digest);
88
89     }
90
91     /**
92      * Generates hmac with data and secret.
93      * <br/>
94      * 
95      * @param data: The data to be signed.
96      * @param secret: The signing key.
97      * @return Hex encoded HMAC signature.
98      * @since SDNO 0.5
99      */
100     public static String mac(final String data, final byte[] secret) {
101         final Key key = new SecretKeySpec(secret, "HmacSHA256");
102         try {
103             return mac(data, key);
104         } catch(final InvalidKeyException e) {
105             LOGGER.error("Invalid key: ", e);
106             return "";
107         }
108     }
109
110 }