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

com.aliyun.auth.signature.signer.SignAlgorithmHmacSHA1 Maven / Gradle / Ivy

The newest version!
package com.aliyun.auth.signature.signer;

import com.aliyun.auth.signature.exception.SignatureException;

import javax.crypto.Mac;
import java.security.NoSuchAlgorithmException;

public enum SignAlgorithmHmacSHA1 {
    HmacSHA1("HmacSHA1");

    private final ThreadLocal reference;
    private final String algorithmName;

    SignAlgorithmHmacSHA1(String algorithmName) {
        this.algorithmName = algorithmName;
        reference = new MacThreadLocal(algorithmName);
    }

    public String getAlgorithmName(){
        return this.algorithmName;
    }

    public Mac getMac() {
        return reference.get();
    }

    private static class MacThreadLocal extends ThreadLocal {
        private final String algorithmName;

        MacThreadLocal(String algorithmName) {
            this.algorithmName = algorithmName;
        }

        @Override
        protected Mac initialValue() {
            try {
                return Mac.getInstance(algorithmName);
            } catch (NoSuchAlgorithmException e) {
                throw new SignatureException("Unable to fetch Mac instance for Algorithm "
                                + algorithmName + e.getMessage());

            }
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy