SPB Git

Repository Public

signkey.go 3.5 KB 112 lines go

1/**2 * =============================================================================3 *                  SP BOUCHER | INFRASTRUCTURE & AUTOMATION4 *             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5 * =============================================================================6 *7 * Project          : MacLustr Command Center (MCC)8 * File             : services/internal/signkey/signkey.go9 * Module           : services10 * Repository       : https://git.spboucher.ai/maclustr-command-center11 * Project Website  : https://clustr.spboucher.ai12 * Created          : 2026-11-1813 * Last Modified    : 2026-11-1814 *15 * Author           : Simon-Pierre Boucher16 * Position         : Professor of Finance17 * Institution      : Université du Québec en Outaouais (UQO)18 * Organization     : SP Boucher19 * Website          : https://spboucher.ai20 * Git              : https://git.spboucher.ai21 *22 * Preferred Contact: admin@spboucher.ai23 * Technical Contact: dev@spboucher.ai24 * Security/Admin   : admin@spboucher.ai25 *26 * Copyright (c) 202627 * Simon-Pierre Boucher28 * All rights reserved where applicable. This file is governed by the29 * applicable project license, copyright notices and relevant law.30 * Existing third-party rights and notices remain unaffected.31 *32 * License          : None specified (no license file in this repository)33 *34 * Do not embed credentials, private keys, access tokens,35 * certificates or confidential personal information.36 *37 * =============================================================================38 * END OF SP BOUCHER SOURCE HEADER39 * =============================================================================40 */4142// Package signkey manages the Ed25519 signing keys kept in the secrets43// directory (command signing, audit export signing). Only public keys ever44// leave the process.45package signkey4647import (48	"crypto/ed25519"49	"crypto/rand"50	"crypto/sha256"51	"encoding/hex"52	"errors"53	"fmt"54	"os"55	"path/filepath"56	"strings"57)5859// ID derives the short identifier of a public key.60func ID(pub ed25519.PublicKey) string {61	h := sha256.Sum256(pub)62	return hex.EncodeToString(h[:8])63}6465// LoadOrCreate reads an Ed25519 seed (hex, mode 0600) or creates it. The66// file is created with O_EXCL so two processes starting together end up with67// the same key.68func LoadOrCreate(path string) (ed25519.PrivateKey, error) {69	read := func() (ed25519.PrivateKey, error) {70		b, err := os.ReadFile(path)71		if err != nil {72			return nil, err73		}74		seed, err := hex.DecodeString(strings.TrimSpace(string(b)))75		if err != nil || len(seed) != ed25519.SeedSize {76			return nil, fmt.Errorf("signkey: %s is not an Ed25519 seed", path)77		}78		if st, err := os.Stat(path); err == nil && st.Mode().Perm()&0o077 != 0 {79			return nil, fmt.Errorf("signkey: %s must not be readable by group or others", path)80		}81		return ed25519.NewKeyFromSeed(seed), nil82	}83	if k, err := read(); err == nil {84		return k, nil85	} else if !errors.Is(err, os.ErrNotExist) {86		return nil, err87	}88	if err := os.MkdirAll(filepath.Dir(path), 0o700); err != nil {89		return nil, err90	}91	seed := make([]byte, ed25519.SeedSize)92	if _, err := rand.Read(seed); err != nil {93		return nil, err94	}95	f, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0o600)96	if errors.Is(err, os.ErrExist) {97		return read() // another process created it first98	}99	if err != nil {100		return nil, err101	}102	if _, err := f.WriteString(hex.EncodeToString(seed) + "\n"); err != nil {103		f.Close()104		os.Remove(path)105		return nil, err106	}107	if err := f.Close(); err != nil {108		return nil, err109	}110	return ed25519.NewKeyFromSeed(seed), nil111}112
Files main
Menu

Theme