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

com.aliyuncs.auth.ShaHmac256 Maven / Gradle / Ivy

There is a newer version: 1.8.10
Show newest version
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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 com.aliyuncs.auth;

import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;

import com.aliyuncs.utils.Base64Helper;

public class ShaHmac256 implements ISigner {

	private final static String AGLORITHM_NAME = "HmacSHA256";
	
	public String signString(String source, String accessSecret)
		throws InvalidKeyException, IllegalStateException {
		try {
			Mac mac = Mac.getInstance(AGLORITHM_NAME);
			mac.init(new SecretKeySpec(
					accessSecret.getBytes(AcsURLEncoder.URL_ENCODING),AGLORITHM_NAME));
			byte[] signData = mac.doFinal(source.getBytes(AcsURLEncoder.URL_ENCODING));
			return Base64Helper.encode(signData);
		} catch (NoSuchAlgorithmException e) {
			throw new RuntimeException("HMAC-SHA1 not supported.");
		} catch (UnsupportedEncodingException e) {
			throw new RuntimeException("UTF-8 not supported.");
		}
        
	}

	public String getSignerName() {
		return "HMAC-SHA256";
	}

	public String getSignerVersion() {
		return "1.0";
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy