mirror of
https://github.com/versia-pub/versia-go.git
synced 2026-03-13 20:49:15 +01:00
refactor!: working WD-4 user discovery
This commit is contained in:
parent
cf0053312d
commit
61891d891a
91 changed files with 12768 additions and 5562 deletions
23
pkg/lysand/crypto/keys.go
Normal file
23
pkg/lysand/crypto/keys.go
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
package versiacrypto
|
||||
|
||||
import (
|
||||
"crypto/ed25519"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
type UnknownPublicKeyTypeError struct {
|
||||
Got string
|
||||
}
|
||||
|
||||
func (i UnknownPublicKeyTypeError) Error() string {
|
||||
return fmt.Sprintf("unknown public key type: \"%s\"", i.Got)
|
||||
}
|
||||
|
||||
func ToTypedKey(algorithm string, raw []byte) (any, error) {
|
||||
switch algorithm {
|
||||
case "ed25519":
|
||||
return ed25519.PublicKey(raw), nil
|
||||
default:
|
||||
return nil, UnknownPublicKeyTypeError{algorithm}
|
||||
}
|
||||
}
|
||||
9
pkg/lysand/crypto/sha256.go
Normal file
9
pkg/lysand/crypto/sha256.go
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
package versiacrypto
|
||||
|
||||
import "crypto/sha256"
|
||||
|
||||
func SHA256(data []byte) []byte {
|
||||
h := sha256.New()
|
||||
h.Write(data)
|
||||
return h.Sum(nil)
|
||||
}
|
||||
29
pkg/lysand/crypto/verify.go
Normal file
29
pkg/lysand/crypto/verify.go
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
package versiacrypto
|
||||
|
||||
import (
|
||||
"crypto"
|
||||
"crypto/ed25519"
|
||||
"fmt"
|
||||
"reflect"
|
||||
)
|
||||
|
||||
type InvalidPublicKeyTypeError struct {
|
||||
Got reflect.Type
|
||||
}
|
||||
|
||||
func (i InvalidPublicKeyTypeError) Error() string {
|
||||
return fmt.Sprintf("failed to convert public key of type \"%s\"", i.Got.String())
|
||||
}
|
||||
|
||||
type Verify = func(data, signature []byte) bool
|
||||
|
||||
func NewVerify(pubKey crypto.PublicKey) (Verify, error) {
|
||||
switch pk := pubKey.(type) {
|
||||
case ed25519.PublicKey:
|
||||
return func(data, signature []byte) bool {
|
||||
return ed25519.Verify(pk, data, signature)
|
||||
}, nil
|
||||
default:
|
||||
return nil, InvalidPublicKeyTypeError{reflect.TypeOf(pk)}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue