Service.java.md
package com.fedoubt.services;
import com.fedoubt.common.message.CustomHttpStatus;
import com.fedoubt.dtos.CryptoDto;
import com.fedoubt.ex.CustomException;
import com.fedoubt.pojos.Crypto;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import java.util.*;
import java.util.stream.Collectors;
@Slf4j
@Service
public class CryptoService {
private final int ITERATIONS = 10000;
private final int KEY_LENGTH = 256;
public void checkdata(CryptoDto cryptoDto ,boolean isDecrypt){
log.info("dto:{}",cryptoDto);
if (cryptoDto == null || StringUtils.isEmpty(cryptoDto.getItemname())
|| StringUtils.isEmpty(cryptoDto.getUsername())
|| StringUtils.isEmpty(cryptoDto.getPassword())) {
// 可以直接丟 ,GlobalExceptionHandler 統一返回
throw new CustomException(CustomHttpStatus.INVALID_REQUEST_DATA);
}
List<String> dataList = Optional.ofNullable(cryptoDto.getDataList()).orElse(Collections.emptyList());
for (String data : dataList){
if(data.length()< 4){
throw new CustomException(CustomHttpStatus.DATA_TOO_SHORT);
}
if(data.length() >= 20){
throw new CustomException(CustomHttpStatus.DATA_TOO_LONG);
}
}
}
public String encrypt(CryptoDto cryptoDto) {
checkdata(cryptoDto ,false);
Crypto crypto = new Crypto();
if(cryptoDto.getItemname().contains(";")){
throw new CustomException(CustomHttpStatus.ENCRYPT_COLON_CONFLICT);
}
}
}