Total Lines | 26 |
Duplicated Lines | 0 % |
Changes | 0 |
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 |