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

io.github.nichetoolkit.rest.worker.RadixWorker Maven / Gradle / Ivy

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

import io.github.nichetoolkit.rest.configure.RestRadixProperties;
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.security.SecureRandom;

/**
 * RadixWorker
 * 

The radix worker class.

* @author Cyan ([email protected]) * @see lombok.extern.slf4j.Slf4j * @since Jdk1.8 */ @Slf4j public class RadixWorker { /** * radixProperties * {@link io.github.nichetoolkit.rest.configure.RestRadixProperties}

The radixProperties field.

* @see io.github.nichetoolkit.rest.configure.RestRadixProperties */ private final RestRadixProperties radixProperties; /** * INSTANCE * {@link io.github.nichetoolkit.rest.worker.RadixWorker}

The constant INSTANCE field.

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

The get instance getter method.

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

The get instance return object is RadixWorker type.

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

Instantiates a new radix worker.

* @param radixProperties {@link io.github.nichetoolkit.rest.configure.RestRadixProperties}

The radix properties parameter is RestRadixProperties type.

* @see io.github.nichetoolkit.rest.configure.RestRadixProperties * @see org.springframework.beans.factory.annotation.Autowired */ @Autowired public RadixWorker(RestRadixProperties radixProperties) { this.radixProperties = radixProperties; } /** * radixWorkerInit *

The radix worker init method.

* @see javax.annotation.PostConstruct */ @PostConstruct public void radixWorkerInit() { log.debug("The radix properties: {}", JsonUtils.parseJson(radixProperties)); INSTANCE = this; } /** * encrypt *

The encrypt method.

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

The source parameter is Long type.

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

The encrypt return object is String type.

* @see java.lang.Long * @see java.lang.String */ public String encrypt(Long source) { return encrypts(source,radixProperties); } /** * decrypt *

The decrypt method.

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

The target parameter is String type.

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

The decrypt return object is Long type.

* @see java.lang.String * @see java.lang.Long */ public Long decrypt(String target) { return decrypts(target,radixProperties); } /** * encrypts *

The encrypts method.

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

The source parameter is Long type.

* @param properties {@link io.github.nichetoolkit.rest.configure.RestRadixProperties}

The properties parameter is RestRadixProperties type.

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

The encrypts return object is String type.

* @see java.lang.Long * @see io.github.nichetoolkit.rest.configure.RestRadixProperties * @see java.lang.String */ private static synchronized String encrypts(Long source, RestRadixProperties properties) { return encrypts(source,properties.toDigitsChar(),properties.toSupplyChar(),properties.getMinLength()); } /** * encrypts *

The encrypts method.

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

The source parameter is Long type.

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

The encrypts return object is String type.

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

The encrypts method.

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

The source parameter is Long type.

* @param digits char

The digits parameter is char type.

* @param supply char

The supply parameter is char type.

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

The encrypts return object is String type.

* @see java.lang.Long * @see java.lang.String */ public static synchronized String encrypts(Long source, char[] digits, char supply) { return encrypts(source,digits,supply,INSTANCE.radixProperties.getMinLength()); } /** * encrypts *

The encrypts method.

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

The source parameter is Long type.

* @param digits char

The digits parameter is char type.

* @param supply char

The supply parameter is char type.

* @param minLength int

The min length parameter is int type.

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

The encrypts return object is String type.

* @see java.lang.Long * @see java.lang.String */ public static synchronized String encrypts(Long source, char[] digits, char supply, int minLength) { int digitsLength = digits.length; char[] buffer = new char[32]; int charPos = 32; while ((source / digitsLength) > 0) { int index = (int) (source % digitsLength); buffer[--charPos] = digits[index]; source /= digitsLength; } buffer[--charPos] = digits[(int) (source % digitsLength)]; String target = new String(buffer, charPos, (32 - charPos)); if (target.length() < minLength) { StringBuilder targetBuilder = new StringBuilder(); targetBuilder.append(supply); SecureRandom secureRandom = new SecureRandom(); for (int i = 1; i < minLength - target.length(); i++) { targetBuilder.append(digits[secureRandom.nextInt(digitsLength)]); } target += targetBuilder.toString(); } return target; } /** * decrypts *

The decrypts method.

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

The target parameter is String type.

* @param properties {@link io.github.nichetoolkit.rest.configure.RestRadixProperties}

The properties parameter is RestRadixProperties type.

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

The decrypts return object is Long type.

* @see java.lang.String * @see io.github.nichetoolkit.rest.configure.RestRadixProperties * @see java.lang.Long */ private static synchronized Long decrypts(String target, RestRadixProperties properties) { return decrypts(target,properties.toDigitsChar(),properties.toSupplyChar()); } /** * decrypts *

The decrypts method.

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

The target parameter is String type.

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

The decrypts return object is Long type.

* @see java.lang.String * @see java.lang.Long */ public static synchronized Long decrypts(String target) { return decrypts(target,INSTANCE.radixProperties); } /** * decrypts *

The decrypts method.

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

The target parameter is String type.

* @param digits char

The digits parameter is char type.

* @param supply char

The supply parameter is char type.

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

The decrypts return object is Long type.

* @see java.lang.String * @see java.lang.Long */ public static synchronized Long decrypts(String target, char[] digits, char supply) { int digitsLength = digits.length; char[] chars = target.toCharArray(); long source = 0L; for (int i = 0; i < chars.length; i++) { int index = 0; for (int j = 0; j < digitsLength; j++) { if (chars[i] == digits[j]) { index = j; break; } } if (chars[i] == supply) { break; } if (i > 0) { source = source * digitsLength + index; } else { source = index; } } return source; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy