added shamir keys
This commit is contained in:
parent
13780f1c74
commit
be0f532628
14 changed files with 238 additions and 43 deletions
|
|
@ -3,7 +3,10 @@
|
|||
package schema
|
||||
|
||||
import (
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"git.giftfish.de/ston1th/keyctl/pkg/api/types"
|
||||
"git.giftfish.de/ston1th/keyctl/pkg/core"
|
||||
|
|
@ -22,6 +25,7 @@ type NewKey struct {
|
|||
Name string `json:"name"`
|
||||
Size int `json:"size"`
|
||||
Encoding core.Encoding `json:"encoding"`
|
||||
Type core.Type `json:"type"`
|
||||
}
|
||||
|
||||
var (
|
||||
|
|
@ -58,13 +62,52 @@ type Key struct {
|
|||
Created int64 `json:"created"`
|
||||
Size int `json:"size"`
|
||||
Encoding core.Encoding `json:"encoding"`
|
||||
Type core.Type `json:"type"`
|
||||
Key string `json:"key,omitempty"`
|
||||
}
|
||||
|
||||
func (k Key) Print(share string) (string, error) {
|
||||
switch k.Type {
|
||||
case core.Shamir:
|
||||
part1, err := k.Encoding.Decode(k.Key)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
part2, err := hex.DecodeString(share)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
dec, err := k.Type.Decode(append(part1, part2...))
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return k.Encoding.EncodeToString(dec), nil
|
||||
}
|
||||
return k.Key, nil
|
||||
}
|
||||
|
||||
type Keys []Key
|
||||
|
||||
func (Keys) Less(a, b Key) bool { return a.Name < b.Name }
|
||||
func (k Keys) Sort() { slices.SortStableFunc(k, k.Less) }
|
||||
func (k Keys) Len() int { return len(k) }
|
||||
func (k Keys) Fields() string {
|
||||
return "ID\t Name\t Created\t Size\t Encoding\t Type"
|
||||
}
|
||||
func (k Keys) Values() []string {
|
||||
vals := make([]string, len(k))
|
||||
for i, v := range k {
|
||||
vals[i] = fmt.Sprintf("%s\t %s\t %s\t %d\t %s\t %s",
|
||||
v.ID,
|
||||
v.Name,
|
||||
timeFmt(v.Created),
|
||||
v.Size,
|
||||
v.Encoding,
|
||||
v.Type,
|
||||
)
|
||||
}
|
||||
return vals
|
||||
}
|
||||
|
||||
func NewKeys(keys core.Keys) (k Keys) {
|
||||
for _, key := range keys {
|
||||
|
|
@ -87,6 +130,24 @@ type Approvals []Approval
|
|||
|
||||
func (Approvals) Less(a, b Approval) bool { return a.Name < b.Name }
|
||||
func (a Approvals) Sort() { slices.SortStableFunc(a, a.Less) }
|
||||
func (a Approvals) Len() int { return len(a) }
|
||||
func (a Approvals) Fields() string {
|
||||
return "ID\t Approval ID\t Name\t Status\t Created\t IP"
|
||||
}
|
||||
func (a Approvals) Values() []string {
|
||||
vals := make([]string, len(a))
|
||||
for i, v := range a {
|
||||
vals[i] = fmt.Sprintf("%s\t %s\t %s\t %s\t %s\t %s",
|
||||
v.ID,
|
||||
v.AID,
|
||||
v.Name,
|
||||
v.Status,
|
||||
timeFmt(v.Created),
|
||||
v.IP,
|
||||
)
|
||||
}
|
||||
return vals
|
||||
}
|
||||
|
||||
func NewApprovals(m types.ApprovalMap) (a Approvals) {
|
||||
for k, v := range m {
|
||||
|
|
@ -102,3 +163,7 @@ func NewApprovals(m types.ApprovalMap) (a Approvals) {
|
|||
a.Sort()
|
||||
return
|
||||
}
|
||||
|
||||
func timeFmt(t int64) string {
|
||||
return time.Unix(t, 0).Format(time.RFC3339)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue