3 Commits

3 changed files with 5 additions and 12 deletions

View File

@@ -40,7 +40,7 @@ pub fn encrypt(data: String) -> String {
/// - `data` 待解密的内容 /// - `data` 待解密的内容
pub fn decrypt(data: String) -> Result<String, SpiralCipherError> { pub fn decrypt(data: String) -> Result<String, SpiralCipherError> {
if !data.starts_with("[") || data.len() <= 21 { if !data.starts_with("[") || data.len() <= 21 {
return Err(SpiralCipherError::CorruptedCipherData); return Ok(data);
} }
let data = data[1..].to_string(); let data = data[1..].to_string();
let key_seed = data[0..20].to_string(); let key_seed = data[0..20].to_string();

View File

@@ -69,13 +69,6 @@ impl HailSerialCodeAlgorithm {
current_time - *HAIL_PERIOD_START current_time - *HAIL_PERIOD_START
} }
/// 判断指定时间戳是否比已经存储的最后一次使用的时间戳要大。否则时间发生了回拨。
fn validate_timestamp(&self, timestamp: i64) -> bool {
let last_timestamp = self.last_timestamp.clone();
let last_timestamp = last_timestamp.lock().unwrap();
timestamp >= *last_timestamp
}
/// 生成一个64位长整型序列ID。 /// 生成一个64位长整型序列ID。
pub fn generate_serial(&self) -> i64 { pub fn generate_serial(&self) -> i64 {
let last_timestamp = self.last_timestamp.clone(); let last_timestamp = self.last_timestamp.clone();
@@ -93,7 +86,7 @@ impl HailSerialCodeAlgorithm {
std::thread::sleep(time::Duration::from_secs(1)); std::thread::sleep(time::Duration::from_secs(1));
continue; continue;
} }
} else if !self.validate_timestamp(timestamp) { } else if timestamp < *last_timestamp {
std::thread::sleep(time::Duration::from_secs(1)); std::thread::sleep(time::Duration::from_secs(1));
continue; continue;
} }

View File

@@ -1,4 +1,4 @@
use chrono::{DateTime, Datelike, Duration, FixedOffset, NaiveDate, NaiveDateTime, Utc}; use chrono::{DateTime, Datelike, Duration, FixedOffset, NaiveDate, NaiveDateTime, TimeZone, Utc};
/// 获取一个类型为`chrono::DateTime<chrono::FixedOffset>`类型的当前日期时间的实例。时间时区将自动被设置为东八区。 /// 获取一个类型为`chrono::DateTime<chrono::FixedOffset>`类型的当前日期时间的实例。时间时区将自动被设置为东八区。
pub fn now_asia_shanghai() -> DateTime<FixedOffset> { pub fn now_asia_shanghai() -> DateTime<FixedOffset> {
@@ -7,7 +7,7 @@ pub fn now_asia_shanghai() -> DateTime<FixedOffset> {
} }
/// 将一个类型为`chrono::DateTime<chrono::Utc>`类型的日期时间转换到指定时区的时间实例。 /// 将一个类型为`chrono::DateTime<chrono::Utc>`类型的日期时间转换到指定时区的时间实例。
pub fn shift_tz(datetime: DateTime<Utc>, zone: i64) -> DateTime<FixedOffset> { pub fn shift_tz<T: TimeZone>(datetime: DateTime<T>, zone: i64) -> DateTime<FixedOffset> {
if zone.is_positive() { if zone.is_positive() {
datetime.with_timezone( datetime.with_timezone(
&FixedOffset::east_opt(Duration::hours(zone.abs()).num_seconds() as i32).unwrap(), &FixedOffset::east_opt(Duration::hours(zone.abs()).num_seconds() as i32).unwrap(),
@@ -20,7 +20,7 @@ pub fn shift_tz(datetime: DateTime<Utc>, zone: i64) -> DateTime<FixedOffset> {
} }
/// 将一个类型为`chrono::DateTime<chrono::Utc>`类型的日期时间转换到东八区的时间实例。 /// 将一个类型为`chrono::DateTime<chrono::Utc>`类型的日期时间转换到东八区的时间实例。
pub fn shift_to_asia_shanghai(datetime: DateTime<Utc>) -> DateTime<FixedOffset> { pub fn shift_to_asia_shanghai<T: TimeZone>(datetime: DateTime<T>) -> DateTime<FixedOffset> {
shift_tz(datetime, 8) shift_tz(datetime, 8)
} }