All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.shiro.biz.utils.HmacSHA256Utils Maven / Gradle / Ivy

The newest version!
/*
 * Copyright (c) 2018, hiwepy (https://github.com/hiwepy).
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package org.apache.shiro.biz.utils;


import org.apache.shiro.lang.codec.Hex;

import javax.crypto.Mac;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.util.List;
import java.util.Map;

/**
 * 

User: Zhang Kaitao *

Date: 14-2-26 *

Version: 1.0 */ public class HmacSHA256Utils { public static String digest(String key, String content) { try { Mac mac = Mac.getInstance("HmacSHA256"); byte[] secretByte = key.getBytes("utf-8"); byte[] dataBytes = content.getBytes("utf-8"); SecretKey secret = new SecretKeySpec(secretByte, "HMACSHA256"); mac.init(secret); byte[] doFinal = mac.doFinal(dataBytes); char[] hexB = Hex.encode(doFinal); return new String(hexB); } catch (Exception e) { throw new RuntimeException(e); } } public static String digest(String key, Map map) { StringBuilder s = new StringBuilder(); for(Object values : map.values()) { if(values instanceof String[]) { for(String value : (String[])values) { s.append(value); } } else if(values instanceof List) { for(String value : (List)values) { s.append(value); } } else { s.append(values); } } return digest(key, s.toString()); } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy