|
@@ -0,0 +1,41 @@
|
|
|
+package com.qmth.boot.core.security.service.impl;
|
|
|
+
|
|
|
+import com.qmth.boot.core.security.service.EncryptKeyProvider;
|
|
|
+import com.qmth.boot.core.security.service.EncryptService;
|
|
|
+import com.qmth.boot.tools.crypto.AES;
|
|
|
+import com.qmth.boot.tools.models.ByteArray;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+import javax.validation.constraints.NotNull;
|
|
|
+
|
|
|
+@Service
|
|
|
+public class DefaultEncryptService implements EncryptService {
|
|
|
+
|
|
|
+ private String key, vector;
|
|
|
+
|
|
|
+ public DefaultEncryptService(@NotNull EncryptKeyProvider encryptKeyProvider) {
|
|
|
+ String keyDigest = ByteArray.sha1(ByteArray.fromString(encryptKeyProvider.getKey()).toBase64()).toHexString();
|
|
|
+ this.key = keyDigest.substring(0, 16);
|
|
|
+ this.vector = keyDigest.substring(keyDigest.length() - 16);
|
|
|
+ }
|
|
|
+
|
|
|
+ public String encrypt(String text) {
|
|
|
+ try {
|
|
|
+ return AES.encrypt(ByteArray.fromString(text).value(), key, vector).toHexString().toLowerCase();
|
|
|
+ } catch (Exception e) {
|
|
|
+ throw new RuntimeException("string encrypt error", e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public String decrypt(String text) {
|
|
|
+ try {
|
|
|
+ return AES.decrypt(ByteArray.fromHexString(text).value(), key, vector).toString();
|
|
|
+ } catch (Exception e) {
|
|
|
+ throw new RuntimeException("string decrypt error", e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public String hash(String text) {
|
|
|
+ return ByteArray.sha1(ByteArray.sha1(text).toHexString() + key + text.length()).toHexString().toLowerCase();
|
|
|
+ }
|
|
|
+}
|