Skip to content

Commit ea93b91

Browse files
Merge hashicorp#10851: archive_file md5 hash attribute
2 parents edef081 + 61fec06 commit ea93b91

2 files changed

Lines changed: 18 additions & 4 deletions

File tree

builtin/providers/archive/data_source_archive_file.go

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package archive
22

33
import (
4+
"crypto/md5"
45
"crypto/sha1"
56
"crypto/sha256"
67
"encoding/base64"
@@ -68,6 +69,12 @@ func dataSourceFile() *schema.Resource {
6869
ForceNew: true,
6970
Description: "Base64 Encoded SHA256 checksum of output file",
7071
},
72+
"output_md5": &schema.Schema{
73+
Type: schema.TypeString,
74+
Computed: true,
75+
ForceNew: true,
76+
Description: "MD5 of output file",
77+
},
7178
},
7279
}
7380
}
@@ -94,13 +101,14 @@ func dataSourceFileRead(d *schema.ResourceData, meta interface{}) error {
94101
return err
95102
}
96103

97-
sha1, base64sha256, err := genFileShas(outputPath)
104+
sha1, base64sha256, md5, err := genFileShas(outputPath)
98105
if err != nil {
99106

100107
return fmt.Errorf("could not generate file checksum sha256: %s", err)
101108
}
102109
d.Set("output_sha", sha1)
103110
d.Set("output_base64sha256", base64sha256)
111+
d.Set("output_md5", md5)
104112

105113
d.Set("output_size", fi.Size())
106114
d.SetId(d.Get("output_sha").(string))
@@ -136,10 +144,10 @@ func archive(d *schema.ResourceData) error {
136144
return nil
137145
}
138146

139-
func genFileShas(filename string) (string, string, error) {
147+
func genFileShas(filename string) (string, string, string, error) {
140148
data, err := ioutil.ReadFile(filename)
141149
if err != nil {
142-
return "", "", fmt.Errorf("could not compute file '%s' checksum: %s", filename, err)
150+
return "", "", "", fmt.Errorf("could not compute file '%s' checksum: %s", filename, err)
143151
}
144152
h := sha1.New()
145153
h.Write([]byte(data))
@@ -150,5 +158,9 @@ func genFileShas(filename string) (string, string, error) {
150158
shaSum := h256.Sum(nil)
151159
sha256base64 := base64.StdEncoding.EncodeToString(shaSum[:])
152160

153-
return sha1, sha256base64, nil
161+
md5 := md5.New()
162+
md5.Write([]byte(data))
163+
md5Sum := hex.EncodeToString(md5.Sum(nil))
164+
165+
return sha1, sha256base64, md5Sum, nil
154166
}

website/source/docs/providers/archive/d/archive_file.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,5 @@ The following attributes are exported:
4848
* `output_sha` - The SHA1 checksum of output archive file.
4949

5050
* `output_base64sha256` - The base64-encoded SHA256 checksum of output archive file.
51+
52+
* `output_md5` - The MD5 checksum of output archive file.

0 commit comments

Comments
 (0)