11package archive
22
33import (
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}
0 commit comments