github.com/mutagen-io/mutagen@v0.18.0-rc1/pkg/synchronization/hashing/algorithm.go (about)

     1  package hashing
     2  
     3  import (
     4  	"crypto/sha1"
     5  	"crypto/sha256"
     6  	"fmt"
     7  	"hash"
     8  )
     9  
    10  // IsDefault indicates whether or not the algorithm is
    11  // Algorithm_AlgorithmDefault.
    12  func (a Algorithm) IsDefault() bool {
    13  	return a == Algorithm_AlgorithmDefault
    14  }
    15  
    16  // MarshalText implements encoding.TextMarshaler.MarshalText.
    17  func (a Algorithm) MarshalText() ([]byte, error) {
    18  	var result string
    19  	switch a {
    20  	case Algorithm_AlgorithmDefault:
    21  	case Algorithm_AlgorithmSHA1:
    22  		result = "sha1"
    23  	case Algorithm_AlgorithmSHA256:
    24  		result = "sha256"
    25  	case Algorithm_AlgorithmXXH128:
    26  		result = "xxh128"
    27  	default:
    28  		result = "unknown"
    29  	}
    30  	return []byte(result), nil
    31  }
    32  
    33  // UnmarshalText implements encoding.TextUnmarshaler.UnmarshalText.
    34  func (a *Algorithm) UnmarshalText(textBytes []byte) error {
    35  	// Convert the bytes to a string.
    36  	text := string(textBytes)
    37  
    38  	// Convert to a hashing algorithm.
    39  	switch text {
    40  	case "sha1":
    41  		*a = Algorithm_AlgorithmSHA1
    42  	case "sha256":
    43  		*a = Algorithm_AlgorithmSHA256
    44  	case "xxh128":
    45  		*a = Algorithm_AlgorithmXXH128
    46  	default:
    47  		return fmt.Errorf("unknown hashing algorithm specification: %s", text)
    48  	}
    49  
    50  	// Success.
    51  	return nil
    52  }
    53  
    54  // AlgorithmSupportStatus encodes support status for a hashing algorithm.
    55  type AlgorithmSupportStatus uint8
    56  
    57  const (
    58  	// AlgorithmSupportStatusUnsupported indicates that an algorithm is
    59  	// completely unsupported.
    60  	AlgorithmSupportStatusUnsupported AlgorithmSupportStatus = iota
    61  	// AlgorithmSupportStatusRequiresLicense indicates that an algorithm is
    62  	// supported but requires a (currently absent) Mutagen Pro license.
    63  	AlgorithmSupportStatusRequiresLicense
    64  	// AlgorithmSupportStatusSupported indicates that an algorithm is fully
    65  	// supported, either due to being supported universally in Mutagen or due to
    66  	// the presence of a Mutagen Pro license.
    67  	AlgorithmSupportStatusSupported
    68  )
    69  
    70  // SupportStatus returns the support status for a particular algorithm.
    71  func (a Algorithm) SupportStatus() AlgorithmSupportStatus {
    72  	switch a {
    73  	case Algorithm_AlgorithmSHA1:
    74  		return AlgorithmSupportStatusSupported
    75  	case Algorithm_AlgorithmSHA256:
    76  		return AlgorithmSupportStatusSupported
    77  	case Algorithm_AlgorithmXXH128:
    78  		return xxh128SupportStatus()
    79  	default:
    80  		return AlgorithmSupportStatusUnsupported
    81  	}
    82  }
    83  
    84  // Description returns a human-readable description of a hashing algorithm.
    85  func (a Algorithm) Description() string {
    86  	switch a {
    87  	case Algorithm_AlgorithmDefault:
    88  		return "Default"
    89  	case Algorithm_AlgorithmSHA1:
    90  		return "SHA-1"
    91  	case Algorithm_AlgorithmSHA256:
    92  		return "SHA-256"
    93  	case Algorithm_AlgorithmXXH128:
    94  		return "XXH128"
    95  	default:
    96  		return "Unknown"
    97  	}
    98  }
    99  
   100  // Factory returns a constructor for the hashing algorithm. If invoked on a
   101  // default or invalid Algorithm value, this method will panic.
   102  func (a Algorithm) Factory() func() hash.Hash {
   103  	switch a {
   104  	case Algorithm_AlgorithmSHA1:
   105  		return sha1.New
   106  	case Algorithm_AlgorithmSHA256:
   107  		return sha256.New
   108  	case Algorithm_AlgorithmXXH128:
   109  		return newXXH128Factory()
   110  	default:
   111  		panic("default or unknown hashing algorithm")
   112  	}
   113  }