Repository: pion/dtls
Pull request #186 (2020-02-06 05:12:54)

Add fingerprint.StringFromHash
Inverse function of fingerprint.HashFromString.

#### Reference issue
https://github.com/pion/webrtc/pull/1004#discussion_r375470812

codecov-io: # [Codecov](https://codecov.io/gh/pion/dtls/pull/186?src=pr&el=h1) Report
> Merging [#186](https://codecov.io/gh/pion/dtls/pull/186?src=pr&el=desc) into [master](https://codecov.io/gh/pion/dtls/commit/51c3debb757066c1ec8ae960d49174c7d1ff04a5?src=pr&el=desc) will **increase** coverage by `0.02%`.
> The diff coverage is `85.71%`.

[![Impacted file tree graph](https://codecov.io/gh/pion/dtls/pull/186/graphs/tree.svg?width=650&token=bcCB2jB88I&height=150&src=pr)](https://codecov.io/gh/pion/dtls/pull/186?src=pr&el=tree)

```diff
@@            Coverage Diff             @@
##           master     #186      +/-   ##
==========================================
+ Coverage   73.85%   73.87%   +0.02%     
==========================================
  Files          62       62              
  Lines        3427     3434       +7     
==========================================
+ Hits         2531     2537       +6     
- Misses        605      606       +1     
  Partials      291      291
```

| Flag | Coverage Δ | |
|---|---|---|
| #go | `73.76% <85.71%> (+0.02%)` | :arrow_up: |
| #wasm | `70% <85.71%> (+0.03%)` | :arrow_up: |

| [Impacted Files](https://codecov.io/gh/pion/dtls/pull/186?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [pkg/crypto/fingerprint/hash.go](https://codecov.io/gh/pion/dtls/pull/186/diff?src=pr&el=tree#diff-cGtnL2NyeXB0by9maW5nZXJwcmludC9oYXNoLmdv) | `90.9% <85.71%> (-9.1%)` | :arrow_down: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/pion/dtls/pull/186?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/pion/dtls/pull/186?src=pr&el=footer). Last update [51c3deb...61715ea](https://codecov.io/gh/pion/dtls/pull/186?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).


Patch:
diff --git a/pkg/crypto/fingerprint/hash.go b/pkg/crypto/fingerprint/hash.go
index 7f336b8..99e7fa5 100644
--- a/pkg/crypto/fingerprint/hash.go
+++ b/pkg/crypto/fingerprint/hash.go
@@ -23,3 +23,13 @@ func HashFromString(s string) (crypto.Hash, error) {
 	}
 	return 0, errInvalidHashAlgorithm
 }
+
+// StringFromHash allows looking up a string representation of the crypto.Hash.
+func StringFromHash(hash crypto.Hash) (string, error) {
+	for s, h := range nameToHash {
+		if h == hash {
+			return s, nil
+		}
+	}
+	return "", errInvalidHashAlgorithm
+}


Test patch:
diff --git a/pkg/crypto/fingerprint/hash_test.go b/pkg/crypto/fingerprint/hash_test.go
index 1280252..e9617b7 100644
--- a/pkg/crypto/fingerprint/hash_test.go
+++ b/pkg/crypto/fingerprint/hash_test.go
@@ -22,3 +22,19 @@ func TestHashFromString(t *testing.T) {
 		}
 	})
 }
+
+func TestStringFromHash_Roundtrip(t *testing.T) {
+	for _, h := range nameToHash {
+		s, err := StringFromHash(h)
+		if err != nil {
+			t.Fatalf("Unexpected error for valid hash algorithm, got '%v'", err)
+		}
+		h2, err := HashFromString(s)
+		if err != nil {
+			t.Fatalf("Unexpected error for valid hash name, got '%v'", err)
+		}
+		if h != h2 {
+			t.Errorf("Hash value doesn't match, expected: 0x%x, got 0x%x", h, h2)
+		}
+	}
+}