This is an automated email from the ASF dual-hosted git repository.

ccollins pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/mynewt-imgmod.git

commit fa5c27974afdf16ae53453446de0449439a11b2f
Author: Christopher Collins <ccoll...@apache.org>
AuthorDate: Fri Feb 28 16:27:56 2020 -0800

    New command: image rehash
---
 cli/image_cmds.go | 40 ++++++++++++++++++++++++++++++++++++++++
 iimg/iimg.go      |  8 ++++----
 2 files changed, 44 insertions(+), 4 deletions(-)

diff --git a/cli/image_cmds.go b/cli/image_cmds.go
index 6ef308e..13f2564 100644
--- a/cli/image_cmds.go
+++ b/cli/image_cmds.go
@@ -693,6 +693,33 @@ func runExtractBodyCmd(cmd *cobra.Command, args []string) {
        }
 }
 
+func runRehashImgCmd(cmd *cobra.Command, args []string) {
+       if len(args) < 1 {
+               ImgmodUsage(cmd, nil)
+       }
+
+       imgFilename := args[0]
+
+       outFilename, err := CalcOutFilename(imgFilename)
+       if err != nil {
+               ImgmodUsage(cmd, err)
+       }
+
+       img, err := readImage(imgFilename)
+       if err != nil {
+               ImgmodUsage(cmd, err)
+       }
+
+       img, err = iimg.RecalcHash(img)
+       if err != nil {
+               ImgmodUsage(nil, err)
+       }
+
+       if err := writeImage(img, outFilename); err != nil {
+               ImgmodUsage(nil, err)
+       }
+}
+
 func AddImageCommands(cmd *cobra.Command) {
        imageCmd := &cobra.Command{
                Use:   "image",
@@ -910,4 +937,17 @@ func AddImageCommands(cmd *cobra.Command) {
        }
 
        imageCmd.AddCommand(extractBodyCmd)
+
+       rehashImgCmd := &cobra.Command{
+               Use:   "rehash <image>",
+               Short: "Calculates an image's hash and replaces its SHA256 TLV",
+               Run:   runRehashImgCmd,
+       }
+
+       rehashImgCmd.PersistentFlags().StringVarP(&OptOutFilename, "outfile", 
"o",
+               "", "File to write to")
+       rehashImgCmd.PersistentFlags().BoolVarP(&OptInPlace, "inplace", "i", 
false,
+               "Replace input file")
+
+       imageCmd.AddCommand(rehashImgCmd)
 }
diff --git a/iimg/iimg.go b/iimg/iimg.go
index 72e86b3..ea9d436 100644
--- a/iimg/iimg.go
+++ b/iimg/iimg.go
@@ -129,8 +129,8 @@ func ExtractSecret(img *image.Image) ([]byte, error) {
        return tlvs[0].Data, nil
 }
 
-func recalcHash(img image.Image) (image.Image, error) {
-       hash, err := img.CalcHash()
+func RecalcHash(img image.Image) (image.Image, error) {
+       hash, err := img.CalcHash(nil)
        if err != nil {
                return img, err
        }
@@ -174,7 +174,7 @@ func DecryptImageFull(img image.Image,
        img.Header.Flags &^= image.IMAGE_F_ENCRYPTED
 
        // The hash needs to be recalculated now that the header has changed.
-       img, err = recalcHash(img)
+       img, err = RecalcHash(img)
        if err != nil {
                return img, err
        }
@@ -208,7 +208,7 @@ func EncryptImageFull(img image.Image,
 
        // The hash needs to be recalculated now that the header has changed.
        var err error
-       img, err = recalcHash(img)
+       img, err = RecalcHash(img)
        if err != nil {
                return img, err
        }

Reply via email to