github.com/pion/dtls/v2@v2.2.12/pkg/crypto/fingerprint/hash.go (about) 1 // SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly> 2 // SPDX-License-Identifier: MIT 3 4 package fingerprint 5 6 import ( 7 "crypto" 8 "errors" 9 "strings" 10 ) 11 12 var errInvalidHashAlgorithm = errors.New("fingerprint: invalid hash algorithm") 13 14 func nameToHash() map[string]crypto.Hash { 15 return map[string]crypto.Hash{ 16 "md5": crypto.MD5, // [RFC3279] 17 "sha-1": crypto.SHA1, // [RFC3279] 18 "sha-224": crypto.SHA224, // [RFC4055] 19 "sha-256": crypto.SHA256, // [RFC4055] 20 "sha-384": crypto.SHA384, // [RFC4055] 21 "sha-512": crypto.SHA512, // [RFC4055] 22 } 23 } 24 25 // HashFromString allows looking up a hash algorithm by it's string representation 26 func HashFromString(s string) (crypto.Hash, error) { 27 if h, ok := nameToHash()[strings.ToLower(s)]; ok { 28 return h, nil 29 } 30 return 0, errInvalidHashAlgorithm 31 } 32 33 // StringFromHash allows looking up a string representation of the crypto.Hash. 34 func StringFromHash(hash crypto.Hash) (string, error) { 35 for s, h := range nameToHash() { 36 if h == hash { 37 return s, nil 38 } 39 } 40 return "", errInvalidHashAlgorithm 41 }