| 1 |
package sprig |
| 2 |
|
| 3 |
import ( |
| 4 |
"crypto/sha1" |
| 5 |
"crypto/sha256" |
| 6 |
"crypto/sha512" |
| 7 |
"encoding/hex" |
| 8 |
"fmt" |
| 9 |
"hash/adler32" |
| 10 |
) |
| 11 |
|
| 12 |
// sha512sum computes the SHA-512 hash of the input string and returns it as a hex-encoded string. |
| 13 |
// This function can be used in templates to generate secure hashes of sensitive data. |
| 14 |
// |
| 15 |
// Example usage in templates: {{ "hello world" | sha512sum }} |
| 16 |
func sha512sum(input string) string { |
| 17 |
hash := sha512.Sum512([]byte(input)) |
| 18 |
return hex.EncodeToString(hash[:]) |
| 19 |
} |
| 20 |
|
| 21 |
// sha256sum computes the SHA-256 hash of the input string and returns it as a hex-encoded string. |
| 22 |
// This is a commonly used cryptographic hash function that produces a 256-bit (32-byte) hash value. |
| 23 |
// |
| 24 |
// Example usage in templates: {{ "hello world" | sha256sum }} |
| 25 |
func sha256sum(input string) string { |
| 26 |
hash := sha256.Sum256([]byte(input)) |
| 27 |
return hex.EncodeToString(hash[:]) |
| 28 |
} |
| 29 |
|
| 30 |
// sha1sum computes the SHA-1 hash of the input string and returns it as a hex-encoded string. |
| 31 |
// Note: SHA-1 is no longer considered secure against well-funded attackers for cryptographic purposes. |
| 32 |
// Consider using sha256sum or sha512sum for security-critical applications. |
| 33 |
// |
| 34 |
// Example usage in templates: {{ "hello world" | sha1sum }} |
| 35 |
func sha1sum(input string) string { |
| 36 |
hash := sha1.Sum([]byte(input)) |
| 37 |
return hex.EncodeToString(hash[:]) |
| 38 |
} |
| 39 |
|
| 40 |
// adler32sum computes the Adler-32 checksum of the input string and returns it as a decimal string. |
| 41 |
// This is a non-cryptographic hash function primarily used for error detection. |
| 42 |
// |
| 43 |
// Example usage in templates: {{ "hello world" | adler32sum }} |
| 44 |
func adler32sum(input string) string { |
| 45 |
hash := adler32.Checksum([]byte(input)) |
| 46 |
return fmt.Sprintf("%d", hash) |
| 47 |
} |