...

Source file src/github.com/sigstore/rekor/cmd/rekor-cli/app/validate.go

Documentation: github.com/sigstore/rekor/cmd/rekor-cli/app

     1  //
     2  // Copyright 2021 The Sigstore Authors.
     3  //
     4  // Licensed under the Apache License, Version 2.0 (the "License");
     5  // you may not use this file except in compliance with the License.
     6  // You may obtain a copy of the License at
     7  //
     8  //     http://www.apache.org/licenses/LICENSE-2.0
     9  //
    10  // Unless required by applicable law or agreed to in writing, software
    11  // distributed under the License is distributed on an "AS IS" BASIS,
    12  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  // See the License for the specific language governing permissions and
    14  // limitations under the License.
    15  
    16  package app
    17  
    18  import (
    19  	"errors"
    20  	"fmt"
    21  	"strings"
    22  
    23  	validator "github.com/asaskevich/govalidator"
    24  )
    25  
    26  // validateSHA512Value ensures that the supplied string matches the
    27  // following format: [sha512:]<128 hexadecimal characters>
    28  // where [sha512:] is optional
    29  func validateSHA512Value(v string) error {
    30  	var prefix, hash string
    31  
    32  	split := strings.SplitN(v, ":", 2)
    33  	switch len(split) {
    34  	case 1:
    35  		hash = split[0]
    36  	case 2:
    37  		prefix = split[0]
    38  		hash = split[1]
    39  	}
    40  
    41  	if strings.TrimSpace(prefix) != "" && prefix != "sha512" {
    42  		return fmt.Errorf("invalid prefix '%v'", prefix)
    43  	}
    44  
    45  	if !validator.IsSHA512(strings.ToLower(hash)) {
    46  		return errors.New("invalid SHA512 value")
    47  	}
    48  	return nil
    49  }
    50  
    51  // validateSHA256Value ensures that the supplied string matches the following format:
    52  // [sha256:]<64 hexadecimal characters>
    53  // where [sha256:] is optional
    54  func validateSHA256Value(v string) error {
    55  	var prefix, hash string
    56  
    57  	split := strings.SplitN(v, ":", 2)
    58  	switch len(split) {
    59  	case 1:
    60  		hash = split[0]
    61  	case 2:
    62  		prefix = split[0]
    63  		hash = split[1]
    64  	}
    65  
    66  	if strings.TrimSpace(prefix) != "" && prefix != "sha256" {
    67  		return fmt.Errorf("invalid prefix '%v'", prefix)
    68  	}
    69  
    70  	if !validator.IsSHA256(strings.ToLower(hash)) {
    71  		return errors.New("invalid SHA256 value")
    72  	}
    73  	return nil
    74  }
    75  
    76  func validateSHA1Value(v string) error {
    77  	var prefix, hash string
    78  
    79  	split := strings.SplitN(v, ":", 2)
    80  	switch len(split) {
    81  	case 1:
    82  		hash = split[0]
    83  	case 2:
    84  		prefix = split[0]
    85  		hash = split[1]
    86  	}
    87  
    88  	if strings.TrimSpace(prefix) != "" && prefix != "sha1" {
    89  		return fmt.Errorf("invalid prefix '%v'", prefix)
    90  	}
    91  
    92  	if !validator.IsSHA1(strings.ToLower(hash)) {
    93  		return errors.New("invalid SHA1 value")
    94  	}
    95  	return nil
    96  }
    97  

View as plain text