-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into 945-cancel-list-on-mount-seconds
- Loading branch information
Showing
54 changed files
with
2,515 additions
and
1,385 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package hash | ||
|
||
import ( | ||
"encoding/binary" | ||
"fmt" | ||
"math" | ||
"reflect" | ||
|
||
yamlk8s "sigs.k8s.io/yaml" | ||
) | ||
|
||
type encoder func(v any) ([]byte, error) | ||
|
||
func boolEncoder(v any) ([]byte, error) { | ||
if reflect.ValueOf(v).Bool() { | ||
return []byte{1}, nil | ||
} | ||
return []byte{0}, nil | ||
} | ||
|
||
func intEncoder(v any) ([]byte, error) { | ||
vt := reflect.ValueOf(v) | ||
return binary.AppendVarint([]byte{}, vt.Int()), nil | ||
} | ||
|
||
func uintEncoder(v any) ([]byte, error) { | ||
vt := reflect.ValueOf(v) | ||
return binary.AppendUvarint([]byte{}, vt.Uint()), nil | ||
} | ||
|
||
func floatEncoder(v any) ([]byte, error) { | ||
vt := reflect.ValueOf(v) | ||
return uintEncoder(math.Float64bits(vt.Float())) | ||
} | ||
|
||
func stringEncoder(v any) ([]byte, error) { | ||
vt := reflect.ValueOf(v) | ||
return []byte(vt.String()), nil | ||
} | ||
|
||
func structEncoder(v any) ([]byte, error) { | ||
b, err := yamlk8s.Marshal(v) | ||
return b, err | ||
} | ||
|
||
func getEncoder(v any) (encoder, error) { | ||
t := reflect.Indirect(reflect.ValueOf(v)) | ||
|
||
switch t.Kind() { | ||
case reflect.Bool: | ||
return boolEncoder, nil | ||
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: | ||
return intEncoder, nil | ||
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: | ||
return uintEncoder, nil | ||
case reflect.Float32, reflect.Float64: | ||
return floatEncoder, nil | ||
case reflect.String: | ||
return stringEncoder, nil | ||
case reflect.Struct, reflect.Map, reflect.Slice, reflect.Array: | ||
return structEncoder, nil | ||
} | ||
return nil, fmt.Errorf("encoder for type %s not supported", t.Kind().String()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package hash | ||
|
||
import ( | ||
"encoding/hex" | ||
"fmt" | ||
"strings" | ||
) | ||
|
||
type Algorithm string | ||
|
||
const ( | ||
hashStringSeparator string = "=" | ||
) | ||
|
||
type sumFunc func(in []byte) []byte | ||
|
||
var sumProviders map[Algorithm]sumFunc = map[Algorithm]sumFunc{} | ||
|
||
func registerProvider(alg Algorithm, provider sumFunc) { | ||
sumProviders[alg] = provider | ||
} | ||
|
||
func ToHashString(alg Algorithm, source any) (string, error) { | ||
sum, found := sumProviders[alg] | ||
if !found { | ||
return "", fmt.Errorf("hash algorithm %s not found", alg) | ||
} | ||
|
||
encode, err := getEncoder(source) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
b, err := encode(source) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
hashBytes := sum(b) | ||
return joinToHashString(alg, hex.EncodeToString(hashBytes)), nil | ||
} | ||
|
||
func CompareWithHashString(source any, targetHashString string) (bool, error) { | ||
alg := extractAlgorithmFromHashString(targetHashString) | ||
sourceHashString, err := ToHashString(alg, source) | ||
if err != nil { | ||
return false, err | ||
} | ||
return targetHashString == sourceHashString, nil | ||
} | ||
|
||
func joinToHashString(alg Algorithm, hash string) string { | ||
return fmt.Sprintf("%s%s%s", alg, hashStringSeparator, hash) | ||
} | ||
|
||
func extractAlgorithmFromHashString(hashString string) Algorithm { | ||
parts := strings.Split(hashString, hashStringSeparator) | ||
if len(parts) != 2 { | ||
return "" | ||
} | ||
return Algorithm(parts[0]) | ||
} |
Oops, something went wrong.