File: md5_checksum.go

package info (click to toggle)
golang-github-aws-smithy-go 1.13.3-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 2,228 kB
  • sloc: java: 12,359; xml: 166; sh: 131; makefile: 47
file content (25 lines) | stat: -rw-r--r-- 615 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
package http

import (
	"crypto/md5"
	"encoding/base64"
	"fmt"
	"io"
)

// computeMD5Checksum computes base64 md5 checksum of an io.Reader's contents.
// Returns the byte slice of md5 checksum and an error.
func computeMD5Checksum(r io.Reader) ([]byte, error) {
	h := md5.New()
	// copy errors may be assumed to be from the body.
	_, err := io.Copy(h, r)
	if err != nil {
		return nil, fmt.Errorf("failed to read body: %w", err)
	}

	// encode the md5 checksum in base64.
	sum := h.Sum(nil)
	sum64 := make([]byte, base64.StdEncoding.EncodedLen(len(sum)))
	base64.StdEncoding.Encode(sum64, sum)
	return sum64, nil
}