5
5
"crypto/md5"
6
6
"crypto/sha1"
7
7
"crypto/sha256"
8
+ "crypto/sha512"
8
9
"encoding/base64"
9
10
"encoding/hex"
10
11
"fmt"
@@ -149,23 +150,35 @@ func (d *archiveFileDataSource) Schema(ctx context.Context, req datasource.Schem
149
150
Description : "The byte size of the output archive file." ,
150
151
Computed : true ,
151
152
},
153
+ "output_file_mode" : schema.StringAttribute {
154
+ Description : "String that specifies the octal file mode for all archived files. For example: `\" 0666\" `. " +
155
+ "Setting this will ensure that cross platform usage of this module will not vary the modes of archived " +
156
+ "files (and ultimately checksums) resulting in more deterministic behavior." ,
157
+ Optional : true ,
158
+ },
159
+ "output_md5" : schema.StringAttribute {
160
+ Description : "MD5 of output file" ,
161
+ Computed : true ,
162
+ },
152
163
"output_sha" : schema.StringAttribute {
153
- Description : "The SHA1 checksum of output archive file." ,
164
+ Description : "SHA1 checksum of output file" ,
165
+ Computed : true ,
166
+ },
167
+ "output_sha256" : schema.StringAttribute {
168
+ Description : "SHA256 checksum of output file" ,
154
169
Computed : true ,
155
170
},
156
171
"output_base64sha256" : schema.StringAttribute {
157
- Description : "The base64-encoded SHA256 checksum of output archive file. " ,
172
+ Description : "Base64 Encoded SHA256 checksum of output file" ,
158
173
Computed : true ,
159
174
},
160
- "output_md5 " : schema.StringAttribute {
161
- Description : "The MD5 checksum of output archive file. " ,
175
+ "output_sha512 " : schema.StringAttribute {
176
+ Description : "SHA512 checksum of output file" ,
162
177
Computed : true ,
163
178
},
164
- "output_file_mode" : schema.StringAttribute {
165
- Description : "String that specifies the octal file mode for all archived files. For example: `\" 0666\" `. " +
166
- "Setting this will ensure that cross platform usage of this module will not vary the modes of archived " +
167
- "files (and ultimately checksums) resulting in more deterministic behavior." ,
168
- Optional : true ,
179
+ "output_base64sha512" : schema.StringAttribute {
180
+ Description : "Base64 Encoded SHA512 checksum of output file" ,
181
+ Computed : true ,
169
182
},
170
183
},
171
184
}
@@ -269,21 +282,23 @@ func (d *archiveFileDataSource) Read(ctx context.Context, req datasource.ReadReq
269
282
)
270
283
return
271
284
}
285
+ model .OutputSize = types .Int64Value (fi .Size ())
272
286
273
- sha1 , base64sha256 , md5 , err := genFileShas (outputPath )
287
+ checksums , err := genFileChecksums (outputPath )
274
288
if err != nil {
275
289
resp .Diagnostics .AddError (
276
290
"Hash generation error" ,
277
- fmt .Sprintf ("error generating hashed : %s" , err ),
291
+ fmt .Sprintf ("error generating checksums : %s" , err ),
278
292
)
279
293
}
294
+ model .OutputMd5 = types .StringValue (checksums .md5Hex )
295
+ model .OutputSha = types .StringValue (checksums .sha1Hex )
296
+ model .OutputSha256 = types .StringValue (checksums .sha256Hex )
297
+ model .OutputBase64Sha256 = types .StringValue (checksums .sha256Base64 )
298
+ model .OutputSha512 = types .StringValue (checksums .sha512Hex )
299
+ model .OutputBase64Sha512 = types .StringValue (checksums .sha512Base64 )
280
300
281
- model .OutputSha = types .StringValue (sha1 )
282
- model .OutputBase64Sha256 = types .StringValue (base64sha256 )
283
- model .OutputMd5 = types .StringValue (md5 )
284
- model .OutputSize = types .Int64Value (fi .Size ())
285
-
286
- model .ID = types .StringValue (sha1 )
301
+ model .ID = types .StringValue (checksums .sha1Hex )
287
302
288
303
diags = resp .State .Set (ctx , model )
289
304
resp .Diagnostics .Append (diags ... )
@@ -304,34 +319,50 @@ type fileModel struct {
304
319
Excludes types.Set `tfsdk:"excludes"`
305
320
OutputPath types.String `tfsdk:"output_path"`
306
321
OutputSize types.Int64 `tfsdk:"output_size"`
322
+ OutputFileMode types.String `tfsdk:"output_file_mode"`
323
+ OutputMd5 types.String `tfsdk:"output_md5"`
307
324
OutputSha types.String `tfsdk:"output_sha"`
325
+ OutputSha256 types.String `tfsdk:"output_sha256"`
308
326
OutputBase64Sha256 types.String `tfsdk:"output_base64sha256"`
309
- OutputMd5 types.String `tfsdk:"output_md5 "`
310
- OutputFileMode types.String `tfsdk:"output_file_mode "`
327
+ OutputSha512 types.String `tfsdk:"output_sha512 "`
328
+ OutputBase64Sha512 types.String `tfsdk:"output_base64sha512 "`
311
329
}
312
330
313
331
type sourceModel struct {
314
332
Content types.String `tfsdk:"content"`
315
333
Filename types.String `tfsdk:"filename"`
316
334
}
317
335
318
- func genFileShas (filename string ) (string , string , string , error ) {
336
+ type fileChecksums struct {
337
+ md5Hex string
338
+ sha1Hex string
339
+ sha256Hex string
340
+ sha256Base64 string
341
+ sha512Hex string
342
+ sha512Base64 string
343
+ }
344
+
345
+ func genFileChecksums (filename string ) (fileChecksums , error ) {
346
+ var checksums fileChecksums
347
+
319
348
data , err := os .ReadFile (filename )
320
349
if err != nil {
321
- return "" , "" , "" , fmt .Errorf ("could not compute file '%s' checksum: %s" , filename , err )
350
+ return checksums , fmt .Errorf ("could not compute file '%s' checksum: %s" , filename , err )
322
351
}
323
- h := sha1 .New ()
324
- h .Write (data )
325
- sha1 := hex .EncodeToString (h .Sum (nil ))
326
352
327
- h256 := sha256 .New ()
328
- h256 .Write (data )
329
- shaSum := h256 .Sum (nil )
330
- sha256base64 := base64 .StdEncoding .EncodeToString (shaSum [:])
353
+ md5Sum := md5 .Sum (data )
354
+ checksums .md5Hex = hex .EncodeToString (md5Sum [:])
355
+
356
+ sha1Sum := sha1 .Sum (data )
357
+ checksums .sha1Hex = hex .EncodeToString (sha1Sum [:])
358
+
359
+ sha256Sum := sha256 .Sum256 (data )
360
+ checksums .sha256Hex = hex .EncodeToString (sha256Sum [:])
361
+ checksums .sha256Base64 = base64 .StdEncoding .EncodeToString (sha256Sum [:])
331
362
332
- md5 := md5 . New ( )
333
- md5 . Write ( data )
334
- md5Sum := hex . EncodeToString (md5 . Sum ( nil ) )
363
+ sha512Sum := sha512 . Sum512 ( data )
364
+ checksums . sha512Hex = hex . EncodeToString ( sha512Sum [:] )
365
+ checksums . sha512Base64 = base64 . StdEncoding . EncodeToString (sha512Sum [:] )
335
366
336
- return sha1 , sha256base64 , md5Sum , nil
367
+ return checksums , nil
337
368
}
0 commit comments