github.com/kovansky/hugo@v0.92.3-0.20220224232819-63076e4ff19f/tpl/crypto/crypto.go (about)

     1  // Copyright 2017 The Hugo Authors. All rights reserved.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  // http://www.apache.org/licenses/LICENSE-2.0
     7  //
     8  // Unless required by applicable law or agreed to in writing, software
     9  // distributed under the License is distributed on an "AS IS" BASIS,
    10  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    11  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  // Package crypto provides template functions for cryptographic operations.
    15  package crypto
    16  
    17  import (
    18  	"crypto/hmac"
    19  	"crypto/md5"
    20  	"crypto/sha1"
    21  	"crypto/sha256"
    22  	"crypto/sha512"
    23  	"encoding/hex"
    24  	"fmt"
    25  	"hash"
    26  
    27  	"github.com/spf13/cast"
    28  )
    29  
    30  // New returns a new instance of the crypto-namespaced template functions.
    31  func New() *Namespace {
    32  	return &Namespace{}
    33  }
    34  
    35  // Namespace provides template functions for the "crypto" namespace.
    36  type Namespace struct{}
    37  
    38  // MD5 hashes the given input and returns its MD5 checksum.
    39  func (ns *Namespace) MD5(in interface{}) (string, error) {
    40  	conv, err := cast.ToStringE(in)
    41  	if err != nil {
    42  		return "", err
    43  	}
    44  
    45  	hash := md5.Sum([]byte(conv))
    46  	return hex.EncodeToString(hash[:]), nil
    47  }
    48  
    49  // SHA1 hashes the given input and returns its SHA1 checksum.
    50  func (ns *Namespace) SHA1(in interface{}) (string, error) {
    51  	conv, err := cast.ToStringE(in)
    52  	if err != nil {
    53  		return "", err
    54  	}
    55  
    56  	hash := sha1.Sum([]byte(conv))
    57  	return hex.EncodeToString(hash[:]), nil
    58  }
    59  
    60  // SHA256 hashes the given input and returns its SHA256 checksum.
    61  func (ns *Namespace) SHA256(in interface{}) (string, error) {
    62  	conv, err := cast.ToStringE(in)
    63  	if err != nil {
    64  		return "", err
    65  	}
    66  
    67  	hash := sha256.Sum256([]byte(conv))
    68  	return hex.EncodeToString(hash[:]), nil
    69  }
    70  
    71  // HMAC returns a cryptographic hash that uses a key to sign a message.
    72  func (ns *Namespace) HMAC(h interface{}, k interface{}, m interface{}) (string, error) {
    73  	ha, err := cast.ToStringE(h)
    74  	if err != nil {
    75  		return "", err
    76  	}
    77  
    78  	var hash func() hash.Hash
    79  	switch ha {
    80  	case "md5":
    81  		hash = md5.New
    82  	case "sha1":
    83  		hash = sha1.New
    84  	case "sha256":
    85  		hash = sha256.New
    86  	case "sha512":
    87  		hash = sha512.New
    88  	default:
    89  		return "", fmt.Errorf("hmac: %s is not a supported hash function", ha)
    90  	}
    91  
    92  	msg, err := cast.ToStringE(m)
    93  	if err != nil {
    94  		return "", err
    95  	}
    96  
    97  	key, err := cast.ToStringE(k)
    98  	if err != nil {
    99  		return "", err
   100  	}
   101  
   102  	mac := hmac.New(hash, []byte(key))
   103  	_, err = mac.Write([]byte(msg))
   104  	if err != nil {
   105  		return "", err
   106  	}
   107  
   108  	return hex.EncodeToString(mac.Sum(nil)[:]), nil
   109  }