This commit is contained in:
Jesse-Ma
2022-12-13 16:23:54 +08:00
parent 5cb7ee80b8
commit 62d7ad4bf8
2 changed files with 31 additions and 9 deletions

View File

@@ -8,6 +8,7 @@ import org.springframework.stereotype.Service;
import com.flagnote.note.entity.Note; import com.flagnote.note.entity.Note;
import com.flagnote.note.repository.NoteMongoRepository; import com.flagnote.note.repository.NoteMongoRepository;
import com.flagnote.note.utils.BizKeyUtils; import com.flagnote.note.utils.BizKeyUtils;
import com.flagnote.note.utils.SecretUtils;
import cn.hutool.core.date.DateUtil; import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ArrayUtil; import cn.hutool.core.util.ArrayUtil;
@@ -20,9 +21,9 @@ public class NoteServiceImpl implements NoteService {
@Override @Override
public Note getNote(String key) { public Note getNote(String key) {
String mixKey = BizKeyUtils.mixKey(key); String mixKey = BizKeyUtils.mixKey(key);
Note note = noteMongoRepository.findById(mixKey).orElse(null); Note note = noteMongoRepository.findById(mixKey).orElse(null);
if (null == note) { if (null == note) {
@@ -40,8 +41,8 @@ public class NoteServiceImpl implements NoteService {
return note; return note;
} }
note.setTextBytes(SecretUtils.decodeNote(note.getTextBytes()));
return note; return note;
} }
@Override @Override
@@ -50,13 +51,14 @@ public class NoteServiceImpl implements NoteService {
note.setId(mixKey); note.setId(mixKey);
note.setKey(mixKey); note.setKey(mixKey);
note.setExpireTime(DateUtil.offsetHour(note.getPushTime(), 1)); note.setExpireTime(DateUtil.offsetHour(note.getPushTime(), 1));
note.setRetentionTime(DateUtil.offsetSecond(note.getPushTime(), 400 * 3600)); note.setRetentionTime(DateUtil.offsetSecond(note.getPushTime(), 400 * 3600));
note.setState(1); note.setState(1);
note.setTextBytes(SecretUtils.encodeNote(note.getTextBytes()));
noteMongoRepository.save(note); noteMongoRepository.save(note);
} }
@@ -64,12 +66,11 @@ public class NoteServiceImpl implements NoteService {
public void deleteNote(String key) { public void deleteNote(String key) {
String mixKey = BizKeyUtils.mixKey(key); String mixKey = BizKeyUtils.mixKey(key);
Note note = noteMongoRepository.findById(mixKey).orElse(null); Note note = noteMongoRepository.findById(mixKey).orElse(null);
if(null!=note && ArrayUtil.isNotEmpty(note.getTextBytes())) { if (null != note && ArrayUtil.isNotEmpty(note.getTextBytes())) {
note.setState(0); note.setState(0);
note.setTextBytes(null); note.setTextBytes(null);
noteMongoRepository.save(note); noteMongoRepository.save(note);
} }
} }
} }

View File

@@ -0,0 +1,21 @@
package com.flagnote.note.utils;
import javax.crypto.spec.SecretKeySpec;
import cn.hutool.crypto.Mode;
import cn.hutool.crypto.Padding;
import cn.hutool.crypto.symmetric.AES;
public class SecretUtils {
private static AES aes = new AES(Mode.ECB, Padding.ZeroPadding,
new SecretKeySpec("b9t3pzbmybc66cba".getBytes(), "AES"));
public static byte[] encodeNote(byte[] text) {
return aes.encrypt(text);
}
public static byte[] decodeNote(byte[] text) {
return aes.decrypt(text);
}
}