Fosi Fossil Service
Small teams. Open source. Built on Fossil.

IaC / SwiftFlow

High-performance WMS with wave picking, smart routing, and real-time inventory visibility.

Public | Owner: IaC | Default branch: trunk | Last update: 2026-10-23 14:37

Artifact ecf21fc1dfd3278bf576d1542593a303d7428231a947c0cfa3d0e4372bc0df85:


import gleam/int
import gleam/string

pub const iterations = 210_000

pub const min_length = 8

pub const max_length = 128

const dummy_hash = "pbkdf2_sha256$210000$00000000000000000000000000000000$0000000000000000000000000000000000000000000000000000000000000000"

@external(erlang, "swiftflow_crypto_ffi", "random_hex")
pub fn random_hex(bytes: Int) -> String

@external(erlang, "swiftflow_crypto_ffi", "sha256_hex")
pub fn sha256_hex(data: String) -> String

@external(erlang, "swiftflow_crypto_ffi", "pbkdf2_sha256_hex")
fn pbkdf2(password: String, salt: String, iterations: Int) -> String

@external(erlang, "swiftflow_crypto_ffi", "secure_equals")
fn secure_equals(a: String, b: String) -> Bool

pub fn validate(password: String) -> Result(String, String) {
  let length = string.length(password)
  case length < min_length, length > max_length {
    True, _ -> Error("密码至少 " <> int.to_string(min_length) <> " 位")
    _, True -> Error("密码不能超过 " <> int.to_string(max_length) <> " 位")
    False, False -> Ok(password)
  }
}

pub fn hash(password: String) -> String {
  hash_with(password, random_hex(16), iterations)
}

pub fn hash_with(password: String, salt: String, rounds: Int) -> String {
  "pbkdf2_sha256$"
  <> int.to_string(rounds)
  <> "$"
  <> salt
  <> "$"
  <> pbkdf2(password, salt, rounds)
}

pub fn verify(password: String, stored: String) -> Bool {
  case string.split(stored, "$") {
    ["pbkdf2_sha256", rounds, salt, expected] ->
      case int.parse(rounds) {
        Ok(rounds) if rounds > 0 ->
          secure_equals(pbkdf2(password, salt, rounds), expected)
        _ -> False
      }
    _ -> False
  }
}

/// 用户不存在时也走一次同等耗时的校验,避免靠响应时间探测账号是否存在。
pub fn burn(password: String) -> Bool {
  let _ = verify(password, dummy_hash)
  False
}

Clone

You can clone this repository with the following command:

fossil clone https://fosi.dev/IaC/SwiftFlow

Contribute

The best way to contribute is through e-mail. See the project documentation for more details.