c5d696556e
三樣都做完並實測過,但落地的最後一步是終端機裡等人親手打字的互動閘。 它們原本只存在於某個 session 的 scratchpad——那種目錄一關就沒了。 · recipes/gitea_put_file.yaml 出貨線 7 站等它 · recipes/cf_worker_deploy_simple.yaml ⚠️ 只適用無 bindings 的簡單情形(見 #90) · hash-component/ sha256/sha1/md5,已與系統原生指令逐位元核對 (.wasm 是 1.3MB 編譯產物,不進版控,README 附重編指令) README 寫了落地指令與各自的注意事項。 Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
90 lines
1.9 KiB
Go
90 lines
1.9 KiB
Go
// hash — 計算內容雜湊(純計算,無網路/檔案 syscall)
|
||
// 支援: sha256, sha1, md5;輸出編碼: hex(預設), base64
|
||
// 用途:出貨線版本號機制(Leo/Arcrun#91)——內容一變雜湊必變,
|
||
// 是「改了東西版本沒動」在結構上不可能發生的機制來源。
|
||
//
|
||
//go:build tinygo
|
||
|
||
package main
|
||
|
||
import (
|
||
"crypto/md5"
|
||
"crypto/sha1"
|
||
"crypto/sha256"
|
||
"encoding/base64"
|
||
"encoding/hex"
|
||
"encoding/json"
|
||
"io"
|
||
"os"
|
||
)
|
||
|
||
type Input struct {
|
||
Algorithm string `json:"algorithm"` // sha256(預設)| sha1 | md5
|
||
Input string `json:"input"`
|
||
Encoding string `json:"encoding"` // hex(預設)| base64
|
||
}
|
||
|
||
func main() {
|
||
raw, err := io.ReadAll(os.Stdin)
|
||
if err != nil {
|
||
writeError("failed to read stdin: " + err.Error())
|
||
return
|
||
}
|
||
var in Input
|
||
if err := json.Unmarshal(raw, &in); err != nil {
|
||
writeError("invalid input JSON: " + err.Error())
|
||
return
|
||
}
|
||
|
||
algorithm := in.Algorithm
|
||
if algorithm == "" {
|
||
algorithm = "sha256"
|
||
}
|
||
encoding := in.Encoding
|
||
if encoding == "" {
|
||
encoding = "hex"
|
||
}
|
||
|
||
var sum []byte
|
||
switch algorithm {
|
||
case "sha256":
|
||
h := sha256.Sum256([]byte(in.Input))
|
||
sum = h[:]
|
||
case "sha1":
|
||
h := sha1.Sum([]byte(in.Input))
|
||
sum = h[:]
|
||
case "md5":
|
||
h := md5.Sum([]byte(in.Input))
|
||
sum = h[:]
|
||
default:
|
||
writeError("不支援的 algorithm: " + algorithm + "(支援 sha256/sha1/md5)")
|
||
return
|
||
}
|
||
|
||
var result string
|
||
switch encoding {
|
||
case "hex":
|
||
result = hex.EncodeToString(sum)
|
||
case "base64":
|
||
result = base64.StdEncoding.EncodeToString(sum)
|
||
default:
|
||
writeError("不支援的 encoding: " + encoding + "(支援 hex/base64)")
|
||
return
|
||
}
|
||
|
||
out, _ := json.Marshal(map[string]interface{}{
|
||
"success": true,
|
||
"data": map[string]interface{}{
|
||
"result": result,
|
||
"algorithm": algorithm,
|
||
"encoding": encoding,
|
||
},
|
||
})
|
||
os.Stdout.Write(out)
|
||
}
|
||
|
||
func writeError(msg string) {
|
||
out, _ := json.Marshal(map[string]interface{}{"success": false, "error": msg})
|
||
os.Stdout.Write(out)
|
||
}
|