sonr/crypto/subtle/subtle.go

131 lines
2.7 KiB
Go
Raw Normal View History

feature/1114 implement account interface (#1167) - **refactor: move session-related code to middleware package** - **refactor: update PKL build process and adjust related configurations** - **feat: integrate base.cosmos.v1 Genesis module** - **refactor: pass session context to modal rendering functions** - **refactor: move nebula package to app directory and update templ version** - **refactor: Move home section video view to dedicated directory** - **refactor: remove unused views file** - **refactor: move styles and UI components to global scope** - **refactor: Rename images.go to cdn.go** - **feat: Add Empty State Illustrations** - **refactor: Consolidate Vault Index Logic** - **fix: References to App.wasm and remove Vault Directory embedded CDN files** - **refactor: Move CDN types to Models** - **fix: Correct line numbers in templ error messages for arch_templ.go** - **refactor: use common types for peer roles** - **refactor: move common types and ORM to a shared package** - **fix: Config import dwn** - **refactor: move nebula directory to app** - **feat: Rebuild nebula** - **fix: correct file paths in panels templates** - **feat: Remove duplicate types** - **refactor: Move dwn to pkg/core** - **refactor: Binary Structure** - **feat: Introduce Crypto Pkg** - **fix: Broken Process Start** - **feat: Update pkg/* structure** - **feat: Refactor PKL Structure** - **build: update pkl build process** - **chore: Remove Empty Files** - **refactor: remove unused macaroon package** - **feat: Add WebAwesome Components** - **refactor: consolidate build and generation tasks into a single taskfile, remove redundant makefile targets** - **refactor: refactor server and move components to pkg/core/dwn** - **build: update go modules** - **refactor: move gateway logic into dedicated hway command** - **feat: Add KSS (Krawczyk-Song-Song) MPC cryptography module** - **feat: Implement MPC-based JWT signing and UCAN token generation** - **feat: add support for MPC-based JWT signing** - **feat: Implement MPC-based UCAN capabilities for smart accounts** - **feat: add address field to keyshareSource** - **feat: Add comprehensive MPC test suite for keyshares, UCAN tokens, and token attenuations** - **refactor: improve MPC keyshare management and signing process** - **feat: enhance MPC capability hierarchy documentation** - **refactor: rename GenerateKeyshares function to NewKeyshareSource for clarity** - **refactor: remove unused Ethereum address computation** - **feat: Add HasHandle and IsAuthenticated methods to HTTPContext** - **refactor: Add context.Context support to session HTTPContext** - **refactor: Resolve context interface conflicts in HTTPContext** - **feat: Add session ID context key and helper functions** - **feat: Update WebApp Page Rendering** - **refactor: Simplify context management by using single HTTPContext key** - **refactor: Simplify HTTPContext creation and context management in session middleware** - **refactor: refactor session middleware to use a single data structure** - **refactor: Simplify HTTPContext implementation and session data handling** - **refactor: Improve session context handling and prevent nil pointer errors** - **refactor: Improve session context handling with nil safety and type support** - **refactor: improve session data injection** - **feat: add full-screen modal component and update registration flow** - **chore: add .air.toml to .gitignore** - **feat: add Air to devbox and update dependencies**
2024-11-23 01:28:58 -05:00
package subtle
import (
"crypto/elliptic"
"crypto/sha1"
"crypto/sha256"
"crypto/sha512"
"encoding/hex"
"errors"
"hash"
"math/big"
)
var errNilHashFunc = errors.New("nil hash function")
// hashDigestSize maps hash algorithms to their digest size in bytes.
var hashDigestSize = map[string]uint32{
"SHA1": uint32(20),
"SHA224": uint32(28),
"SHA256": uint32(32),
"SHA384": uint32(48),
"SHA512": uint32(64),
}
// GetHashDigestSize returns the digest size of the specified hash algorithm.
func GetHashDigestSize(hash string) (uint32, error) {
digestSize, ok := hashDigestSize[hash]
if !ok {
return 0, errors.New("invalid hash algorithm")
}
return digestSize, nil
}
// TODO(ckl): Perhaps return an explicit error instead of ""/nil for the
// following functions.
// ConvertHashName converts different forms of a hash name to the
// hash name that tink recognizes.
func ConvertHashName(name string) string {
switch name {
case "SHA-224":
return "SHA224"
case "SHA-256":
return "SHA256"
case "SHA-384":
return "SHA384"
case "SHA-512":
return "SHA512"
case "SHA-1":
return "SHA1"
default:
return ""
}
}
// ConvertCurveName converts different forms of a curve name to the
// name that tink recognizes.
func ConvertCurveName(name string) string {
switch name {
case "secp256r1", "P-256":
return "NIST_P256"
case "secp384r1", "P-384":
return "NIST_P384"
case "secp521r1", "P-521":
return "NIST_P521"
default:
return ""
}
}
// GetHashFunc returns the corresponding hash function of the given hash name.
func GetHashFunc(hash string) func() hash.Hash {
switch hash {
case "SHA1":
return sha1.New
case "SHA224":
return sha256.New224
case "SHA256":
return sha256.New
case "SHA384":
return sha512.New384
case "SHA512":
return sha512.New
default:
return nil
}
}
// GetCurve returns the curve object that corresponds to the given curve type.
// It returns null if the curve type is not supported.
func GetCurve(curve string) elliptic.Curve {
switch curve {
case "NIST_P256":
return elliptic.P256()
case "NIST_P384":
return elliptic.P384()
case "NIST_P521":
return elliptic.P521()
default:
return nil
}
}
// ComputeHash calculates a hash of the given data using the given hash function.
func ComputeHash(hashFunc func() hash.Hash, data []byte) ([]byte, error) {
if hashFunc == nil {
return nil, errNilHashFunc
}
h := hashFunc()
_, err := h.Write(data)
if err != nil {
return nil, err
}
return h.Sum(nil), nil
}
// NewBigIntFromHex returns a big integer from a hex string.
func NewBigIntFromHex(s string) (*big.Int, error) {
if len(s)%2 == 1 {
s = "0" + s
}
b, err := hex.DecodeString(s)
if err != nil {
return nil, err
}
ret := new(big.Int).SetBytes(b)
return ret, nil
}