1// Copyright (c) Microsoft Corporation. All rights reserved.
2// Licensed under the MIT License.
3
4package azidentity
5
6import (
7	"bytes"
8	"crypto/sha1"
9	"encoding/pem"
10	"fmt"
11)
12
13// fingerprint type wraps a byte slice that contains the corresponding SHA-1 fingerprint for the client's certificate
14type fingerprint []byte
15
16// String represents the fingerprint digest as a series of
17// colon-delimited hexadecimal octets.
18func (f fingerprint) String() string {
19	var buf bytes.Buffer
20	for i, b := range f {
21		if i > 0 {
22			fmt.Fprintf(&buf, ":")
23		}
24		fmt.Fprintf(&buf, "%02x", b)
25	}
26	return buf.String()
27}
28
29// newFingerprint calculates the fingerprint of the certificate based on it's Subject Public Key Info with the SHA-1
30// signing algorithm.
31func newFingerprint(block *pem.Block) (fingerprint, error) {
32	h := sha1.New()
33	_, err := h.Write(block.Bytes)
34	if err != nil {
35		return nil, err
36	}
37	return fingerprint(h.Sum(nil)), nil
38}
39