Skip to content

Commit fa5c279

Browse files
committed
New command: image rehash
1 parent 0f5f9ea commit fa5c279

File tree

2 files changed

+44
-4
lines changed

2 files changed

+44
-4
lines changed

cli/image_cmds.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -693,6 +693,33 @@ func runExtractBodyCmd(cmd *cobra.Command, args []string) {
693693
}
694694
}
695695

696+
func runRehashImgCmd(cmd *cobra.Command, args []string) {
697+
if len(args) < 1 {
698+
ImgmodUsage(cmd, nil)
699+
}
700+
701+
imgFilename := args[0]
702+
703+
outFilename, err := CalcOutFilename(imgFilename)
704+
if err != nil {
705+
ImgmodUsage(cmd, err)
706+
}
707+
708+
img, err := readImage(imgFilename)
709+
if err != nil {
710+
ImgmodUsage(cmd, err)
711+
}
712+
713+
img, err = iimg.RecalcHash(img)
714+
if err != nil {
715+
ImgmodUsage(nil, err)
716+
}
717+
718+
if err := writeImage(img, outFilename); err != nil {
719+
ImgmodUsage(nil, err)
720+
}
721+
}
722+
696723
func AddImageCommands(cmd *cobra.Command) {
697724
imageCmd := &cobra.Command{
698725
Use: "image",
@@ -910,4 +937,17 @@ func AddImageCommands(cmd *cobra.Command) {
910937
}
911938

912939
imageCmd.AddCommand(extractBodyCmd)
940+
941+
rehashImgCmd := &cobra.Command{
942+
Use: "rehash <image>",
943+
Short: "Calculates an image's hash and replaces its SHA256 TLV",
944+
Run: runRehashImgCmd,
945+
}
946+
947+
rehashImgCmd.PersistentFlags().StringVarP(&OptOutFilename, "outfile", "o",
948+
"", "File to write to")
949+
rehashImgCmd.PersistentFlags().BoolVarP(&OptInPlace, "inplace", "i", false,
950+
"Replace input file")
951+
952+
imageCmd.AddCommand(rehashImgCmd)
913953
}

iimg/iimg.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,8 @@ func ExtractSecret(img *image.Image) ([]byte, error) {
129129
return tlvs[0].Data, nil
130130
}
131131

132-
func recalcHash(img image.Image) (image.Image, error) {
133-
hash, err := img.CalcHash()
132+
func RecalcHash(img image.Image) (image.Image, error) {
133+
hash, err := img.CalcHash(nil)
134134
if err != nil {
135135
return img, err
136136
}
@@ -174,7 +174,7 @@ func DecryptImageFull(img image.Image,
174174
img.Header.Flags &^= image.IMAGE_F_ENCRYPTED
175175

176176
// The hash needs to be recalculated now that the header has changed.
177-
img, err = recalcHash(img)
177+
img, err = RecalcHash(img)
178178
if err != nil {
179179
return img, err
180180
}
@@ -208,7 +208,7 @@ func EncryptImageFull(img image.Image,
208208

209209
// The hash needs to be recalculated now that the header has changed.
210210
var err error
211-
img, err = recalcHash(img)
211+
img, err = RecalcHash(img)
212212
if err != nil {
213213
return img, err
214214
}

0 commit comments

Comments
 (0)