com.ksyun.api.sdk.auth.ShaHmac1 Maven / Gradle / Ivy
package com.ksyun.api.sdk.auth;
import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import com.ksyun.api.sdk.utils.Base64Helper;
public class ShaHmac1 implements ISigner {
private final static String AGLORITHM_NAME = "HmacSHA1";
public String signString(String source, String accessSecret)
throws InvalidKeyException, IllegalStateException {
try {
Mac mac = Mac.getInstance(AGLORITHM_NAME);
mac.init(new SecretKeySpec(
accessSecret.getBytes(KscURLEncoder.URL_ENCODING),AGLORITHM_NAME));
byte[] signData = mac.doFinal(source.getBytes(KscURLEncoder.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-SHA1";
}
public String getSignerVersion() {
return "1.0";
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy