| 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 v and returns its MD5 checksum. |
| 39 |
func (ns *Namespace) MD5(v any) (string, error) { |
| 40 |
conv, err := cast.ToStringE(v) |
| 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 v and returns its SHA1 checksum. |
| 50 |
func (ns *Namespace) SHA1(v any) (string, error) { |
| 51 |
conv, err := cast.ToStringE(v) |
| 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 v and returns its SHA256 checksum. |
| 61 |
func (ns *Namespace) SHA256(v any) (string, error) { |
| 62 |
conv, err := cast.ToStringE(v) |
| 63 |
if err != nil { |
| 64 |
return "", err |
| 65 |
} |
| 66 |
|
| 67 |
hash := sha256.Sum256([]byte(conv)) |
| 68 |
return hex.EncodeToString(hash[:]), nil |
| 69 |
} |
| 70 |
|
| 71 |
// Hash returns the hex-encoded checksum of v using the given algorithm; one of |
| 72 |
// md5, sha1, sha256 (the default), sha384 or sha512. |
| 73 |
// |
| 74 |
// The supported algorithms match those used for the Subresource Integrity (SRI) |
| 75 |
// hash in .Data.Integrity on fingerprinted resources, so an SRI hash can be |
| 76 |
// constructed by combining this with encoding.HexDecode and encoding.Base64Encode. |
| 77 |
func (ns *Namespace) Hash(args ...any) (string, error) { |
| 78 |
var algo, v any |
| 79 |
switch len(args) { |
| 80 |
case 1: |
| 81 |
algo, v = "sha256", args[0] |
| 82 |
case 2: |
| 83 |
algo, v = args[0], args[1] |
| 84 |
default: |
| 85 |
return "", fmt.Errorf("crypto.Hash: expected 1 or 2 arguments, got %d", len(args)) |
| 86 |
} |
| 87 |
|
| 88 |
conv, err := cast.ToStringE(v) |
| 89 |
if err != nil { |
| 90 |
return "", err |
| 91 |
} |
| 92 |
|
| 93 |
algoS, err := cast.ToStringE(algo) |
| 94 |
if err != nil { |
| 95 |
return "", err |
| 96 |
} |
| 97 |
|
| 98 |
h, err := newHash(algoS) |
| 99 |
if err != nil { |
| 100 |
return "", err |
| 101 |
} |
| 102 |
|
| 103 |
if _, err := h.Write([]byte(conv)); err != nil { |
| 104 |
return "", err |
| 105 |
} |
| 106 |
|
| 107 |
return hex.EncodeToString(h.Sum(nil)), nil |
| 108 |
} |
| 109 |
|
| 110 |
func newHash(algo string) (hash.Hash, error) { |
| 111 |
switch algo { |
| 112 |
case "md5": |
| 113 |
return md5.New(), nil |
| 114 |
case "sha1": |
| 115 |
return sha1.New(), nil |
| 116 |
case "sha256": |
| 117 |
return sha256.New(), nil |
| 118 |
case "sha384": |
| 119 |
return sha512.New384(), nil |
| 120 |
case "sha512": |
| 121 |
return sha512.New(), nil |
| 122 |
default: |
| 123 |
return nil, fmt.Errorf("crypto.Hash: %q is not a supported hash algorithm", algo) |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
// HMAC returns a cryptographic hash that uses a key to sign a message. |
| 128 |
func (ns *Namespace) HMAC(h any, k any, m any, e ...any) (string, error) { |
| 129 |
ha, err := cast.ToStringE(h) |
| 130 |
if err != nil { |
| 131 |
return "", err |
| 132 |
} |
| 133 |
|
| 134 |
var hash func() hash.Hash |
| 135 |
switch ha { |
| 136 |
case "md5": |
| 137 |
hash = md5.New |
| 138 |
case "sha1": |
| 139 |
hash = sha1.New |
| 140 |
case "sha256": |
| 141 |
hash = sha256.New |
| 142 |
case "sha512": |
| 143 |
hash = sha512.New |
| 144 |
default: |
| 145 |
return "", fmt.Errorf("hmac: %s is not a supported hash function", ha) |
| 146 |
} |
| 147 |
|
| 148 |
msg, err := cast.ToStringE(m) |
| 149 |
if err != nil { |
| 150 |
return "", err |
| 151 |
} |
| 152 |
|
| 153 |
key, err := cast.ToStringE(k) |
| 154 |
if err != nil { |
| 155 |
return "", err |
| 156 |
} |
| 157 |
|
| 158 |
mac := hmac.New(hash, []byte(key)) |
| 159 |
_, err = mac.Write([]byte(msg)) |
| 160 |
if err != nil { |
| 161 |
return "", err |
| 162 |
} |
| 163 |
|
| 164 |
encoding := "hex" |
| 165 |
if len(e) > 0 && e[0] != nil { |
| 166 |
encoding, err = cast.ToStringE(e[0]) |
| 167 |
if err != nil { |
| 168 |
return "", err |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
switch encoding { |
| 173 |
case "binary": |
| 174 |
return string(mac.Sum(nil)[:]), nil |
| 175 |
case "hex": |
| 176 |
return hex.EncodeToString(mac.Sum(nil)[:]), nil |
| 177 |
default: |
| 178 |
return "", fmt.Errorf("%q is not a supported encoding method", encoding) |
| 179 |
} |
| 180 |
} |