SPB Git

Repository Public

secret.go 3.0 KB 90 lines go

1/**2 * =============================================================================3 *                  SP BOUCHER | INFRASTRUCTURE & AUTOMATION4 *             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5 * =============================================================================6 *7 * Project          : MacLustr Command Center (MCC)8 * File             : services/internal/secret/secret.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 secret creates and compares bearer secrets. Only SHA-256 hashes of43// tokens are ever stored; the clear value is shown to its recipient once.44package secret4546import (47	"crypto/rand"48	"crypto/sha256"49	"crypto/subtle"50	"encoding/base64"51	"errors"52)5354// TokenBytes is the entropy of every token (256 bits).55const TokenBytes = 325657// NewToken returns a URL-safe random token and its storage hash.58func NewToken() (clear string, hash []byte) {59	b := make([]byte, TokenBytes)60	if _, err := rand.Read(b); err != nil {61		panic("secret: entropy source failed: " + err.Error())62	}63	clear = base64.RawURLEncoding.EncodeToString(b)64	return clear, Hash(clear)65}6667// Hash returns the storage hash of a clear token.68func Hash(clear string) []byte {69	h := sha256.Sum256([]byte(clear))70	return h[:]71}7273// ErrMalformed is returned for tokens that cannot have been issued by NewToken.74var ErrMalformed = errors.New("malformed token")7576// Parse validates the shape of a presented token (cheap rejection before any77// database lookup) and returns its hash.78func Parse(clear string) ([]byte, error) {79	if len(clear) != base64.RawURLEncoding.EncodedLen(TokenBytes) {80		return nil, ErrMalformed81	}82	if _, err := base64.RawURLEncoding.DecodeString(clear); err != nil {83		return nil, ErrMalformed84	}85	return Hash(clear), nil86}8788// Equal compares two hashes in constant time.89func Equal(a, b []byte) bool { return subtle.ConstantTimeCompare(a, b) == 1 }90
Files main
Menu

Theme