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

io.github.nichetoolkit.rest.worker.sha.ShaWorker Maven / Gradle / Ivy

The newest version!
package io.github.nichetoolkit.rest.worker.sha;

import io.github.nichetoolkit.rest.configure.RestShaProperties;
import io.github.nichetoolkit.rest.util.JsonUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.PostConstruct;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * ShaWorker
 * 

The sha worker class.

* @author Cyan ([email protected]) * @see lombok.extern.slf4j.Slf4j * @since Jdk1.8 */ @Slf4j public class ShaWorker { /** * SIGN_KEY * {@link java.lang.String}

The constant SIGN_KEY field.

* @see java.lang.String */ private static final String SIGN_KEY = "sign"; /** * PASSWORD_KEY * {@link java.lang.String}

The constant PASSWORD_KEY field.

* @see java.lang.String */ private static final String PASSWORD_KEY = "password"; /** * shaProperties * {@link io.github.nichetoolkit.rest.configure.RestShaProperties}

The shaProperties field.

* @see io.github.nichetoolkit.rest.configure.RestShaProperties */ private final RestShaProperties shaProperties; /** * INSTANCE * {@link io.github.nichetoolkit.rest.worker.sha.ShaWorker}

The constant INSTANCE field.

*/ private static ShaWorker INSTANCE = null; /** * getInstance *

The get instance getter method.

* @return {@link io.github.nichetoolkit.rest.worker.sha.ShaWorker}

The get instance return object is ShaWorker type.

*/ public static ShaWorker getInstance() { return INSTANCE; } /** * ShaWorker *

Instantiates a new sha worker.

* @param shaProperties {@link io.github.nichetoolkit.rest.configure.RestShaProperties}

The sha properties parameter is RestShaProperties type.

* @see io.github.nichetoolkit.rest.configure.RestShaProperties * @see org.springframework.beans.factory.annotation.Autowired */ @Autowired public ShaWorker(RestShaProperties shaProperties) { this.shaProperties = shaProperties; } /** * shaWorkerInit *

The sha worker init method.

* @see javax.annotation.PostConstruct */ @PostConstruct public void shaWorkerInit() { log.debug("The sha properties: {}", JsonUtils.parseJson(shaProperties)); INSTANCE = this; } /** * shaEncrypt *

The sha encrypt method.

* @param source {@link java.lang.String}

The source parameter is String type.

* @return {@link java.lang.String}

The sha encrypt return object is String type.

* @see java.lang.String */ private static String shaEncrypt(String source) { return encrypt(source,INSTANCE.shaProperties.getAlgorithm()); } /** * encrypt *

The encrypt method.

* @param source {@link java.lang.String}

The source parameter is String type.

* @param algorithm {@link io.github.nichetoolkit.rest.worker.sha.ShaAlgorithm}

The algorithm parameter is ShaAlgorithm type.

* @return {@link java.lang.String}

The encrypt return object is String type.

* @see java.lang.String * @see io.github.nichetoolkit.rest.worker.sha.ShaAlgorithm */ public static String encrypt(String source, ShaAlgorithm algorithm) { StringBuilder hexBuilder = new StringBuilder(); try { MessageDigest messageDigest = MessageDigest.getInstance(algorithm.getAlgorithm()); byte[] bytes = messageDigest.digest(source.getBytes(StandardCharsets.UTF_8)); for (byte byt : bytes) { hexBuilder.append(Integer.toHexString((byt & 0xFF) | 0x100), 1, 3); } } catch (NoSuchAlgorithmException exception) { log.error("the encrypts algorithm of {} is error !, error: {}", algorithm, exception.getMessage()); } return hexBuilder.toString().toUpperCase(); } /** * encrypt *

The encrypt method.

* @param source {@link java.lang.String}

The source parameter is String type.

* @return {@link java.lang.String}

The encrypt return object is String type.

* @see java.lang.String */ public String encrypt(String source) { return encrypts(source, this.shaProperties.getSecret()); } /** * encrypts *

The encrypts method.

* @param source {@link java.lang.String}

The source parameter is String type.

* @return {@link java.lang.String}

The encrypts return object is String type.

* @see java.lang.String */ public static String encrypts(String source) { return encrypts(source,INSTANCE.shaProperties.getSecret()); } /** * encrypts *

The encrypts method.

* @param source {@link java.lang.String}

The source parameter is String type.

* @param secret {@link java.lang.String}

The secret parameter is String type.

* @return {@link java.lang.String}

The encrypts return object is String type.

* @see java.lang.String */ public static String encrypts(String source, String secret) { Map paramMap = new HashMap<>(); String target = shaEncrypt(source); paramMap.put(PASSWORD_KEY, target); return encrypts(paramMap, secret); } /** * encrypts *

The encrypts method.

* @param source {@link java.util.Map}

The source parameter is Map type.

* @param secret {@link java.lang.String}

The secret parameter is String type.

* @return {@link java.lang.String}

The encrypts return object is String type.

* @see java.util.Map * @see java.lang.String */ public static String encrypts(final Map source, String secret) { Set keySet = source.keySet(); String[] keyArray = keySet.toArray(new String[0]); Arrays.sort(keyArray); StringBuilder keyBuilder = new StringBuilder(); for (String key : keyArray) { if (SIGN_KEY.equals(key)) { continue; } if("v".equals(key)){ continue; } if (source.get(key) != null && !source.get(key).toString().trim().isEmpty()) { keyBuilder.append(key).append("=").append(source.get(key).toString().trim()).append("&"); } } keyBuilder.append("key=").append(secret); return shaEncrypt(keyBuilder.toString()).toUpperCase(); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy