internal/md5.go   A
last analyzed

Size/Duplication

Total Lines 26
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 15
dl 0
loc 26
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A internal.Md5 0 5 2
A internal.md5String 0 6 2
1
package internal
2
3
import (
4
	"crypto/md5"
5
	"fmt"
6
7
	"github.com/gotilty/gotil/internal/errs"
8
)
9
10
//MD5 returns the MD5 checksum of the data.
11
//Just works with all primitive types.
12
//If given parameter is different type instead of string, firstly, it will convert the given parameter to string with gotil.ToString()
13
func Md5(a interface{}) (string, error) {
14
	if s, err := ToString(a); err == nil {
15
		return md5String(s)
16
	} else {
17
		return "", err
18
	}
19
}
20
21
func md5String(s string) (string, error) {
22
	if s == "" {
23
		return "", errs.NilReferenceTypeError()
24
	}
25
	data := []byte(s)
26
	return fmt.Sprintf("%x", md5.Sum(data)), nil
27
}
28