github.com/anakojm/hugo-katex@v0.0.0-20231023141351-42d6f5de9c0b/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 "hash/fnv" 27 28 "github.com/spf13/cast" 29 ) 30 31 // New returns a new instance of the crypto-namespaced template functions. 32 func New() *Namespace { 33 return &Namespace{} 34 } 35 36 // Namespace provides template functions for the "crypto" namespace. 37 type Namespace struct{} 38 39 // MD5 hashes the v and returns its MD5 checksum. 40 func (ns *Namespace) MD5(v any) (string, error) { 41 conv, err := cast.ToStringE(v) 42 if err != nil { 43 return "", err 44 } 45 46 hash := md5.Sum([]byte(conv)) 47 return hex.EncodeToString(hash[:]), nil 48 } 49 50 // SHA1 hashes v and returns its SHA1 checksum. 51 func (ns *Namespace) SHA1(v any) (string, error) { 52 conv, err := cast.ToStringE(v) 53 if err != nil { 54 return "", err 55 } 56 57 hash := sha1.Sum([]byte(conv)) 58 return hex.EncodeToString(hash[:]), nil 59 } 60 61 // SHA256 hashes v and returns its SHA256 checksum. 62 func (ns *Namespace) SHA256(v any) (string, error) { 63 conv, err := cast.ToStringE(v) 64 if err != nil { 65 return "", err 66 } 67 68 hash := sha256.Sum256([]byte(conv)) 69 return hex.EncodeToString(hash[:]), nil 70 } 71 72 // FNV32a hashes v using fnv32a algorithm. 73 // <docsmeta>{"newIn": "0.98.0" }</docsmeta> 74 func (ns *Namespace) FNV32a(v any) (int, error) { 75 conv, err := cast.ToStringE(v) 76 if err != nil { 77 return 0, err 78 } 79 algorithm := fnv.New32a() 80 algorithm.Write([]byte(conv)) 81 return int(algorithm.Sum32()), nil 82 } 83 84 // HMAC returns a cryptographic hash that uses a key to sign a message. 85 func (ns *Namespace) HMAC(h any, k any, m any, e ...any) (string, error) { 86 ha, err := cast.ToStringE(h) 87 if err != nil { 88 return "", err 89 } 90 91 var hash func() hash.Hash 92 switch ha { 93 case "md5": 94 hash = md5.New 95 case "sha1": 96 hash = sha1.New 97 case "sha256": 98 hash = sha256.New 99 case "sha512": 100 hash = sha512.New 101 default: 102 return "", fmt.Errorf("hmac: %s is not a supported hash function", ha) 103 } 104 105 msg, err := cast.ToStringE(m) 106 if err != nil { 107 return "", err 108 } 109 110 key, err := cast.ToStringE(k) 111 if err != nil { 112 return "", err 113 } 114 115 mac := hmac.New(hash, []byte(key)) 116 _, err = mac.Write([]byte(msg)) 117 if err != nil { 118 return "", err 119 } 120 121 var encoding = "hex" 122 if len(e) > 0 && e[0] != nil { 123 encoding, err = cast.ToStringE(e[0]) 124 if err != nil { 125 return "", err 126 } 127 } 128 129 switch encoding { 130 case "binary": 131 return string(mac.Sum(nil)[:]), nil 132 case "hex": 133 return hex.EncodeToString(mac.Sum(nil)[:]), nil 134 default: 135 return "", fmt.Errorf("%q is not a supported encoding method", encoding) 136 } 137 138 }