GithubHelp home page GithubHelp logo

Implement signatures about go-appimage HOT 3 CLOSED

probonopd avatar probonopd commented on August 26, 2024
Implement signatures

from go-appimage.

Comments (3)

probonopd avatar probonopd commented on August 26, 2024
package main

import (
	"bytes"
	"crypto/sha256"
	"fmt"
	"hash"
	"io"
	"log"
	"os"
	"sort"
	"strconv"
)

type byteRange struct {
	offset int64
	length int64
}

func main() {
	f, err := os.Open("file.txt")
	if err != nil {
		log.Fatal(err)
	}
	defer f.Close()

	h := CalculateDigestSkippingRanges(f, []byteRange{byteRange{5, 5}, byteRange{15, 5}})

	fmt.Printf("%x\n", h.Sum(nil))

	fmt.Println("Compare to doing it in one go")
	fi, err := f.Stat()
	if err != nil {
		log.Fatal(err)
	}
	h = sha256.New()
	s := io.NewSectionReader(f, 0, fi.Size())
	if _, err := io.Copy(h, s); err != nil {
		log.Fatal(err)
	}

	fmt.Printf("%x\n", h.Sum(nil))
}

func CalculateDigestSkippingRanges(f *os.File, ranges []byteRange) hash.Hash {

	fi, err := f.Stat()
	if err != nil {
		log.Fatal(err)
	}

	fmt.Printf("The file is %d bytes long\n", fi.Size())

	// Sort the ranges by offset, so that we can work on them one after another
	sort.Slice(ranges, func(i, j int) bool {
		return ranges[i].offset < ranges[j].offset
	})

	h := sha256.New()

	// Add to the hash the checksum for the area between offset 0 and the first byteRange
	hashRange(f, h, 0, ranges[0].offset)

	// Add to the hash the checksum for each range to be excluded
	numberOfRanges := len(ranges)
	for i, byterange := range ranges {
		fmt.Println("range offset", byterange.offset, "length", byterange.length)
		hashDummyRange(h, int(byterange.length))
		// Add to the hash the checksum for the area after the excluded range until the end of the file
		if i == numberOfRanges-1 {
			// This was the last excluded range, so we continue to the end of the file
			hashRange(f, h, byterange.offset+byterange.length, fi.Size()-(byterange.offset+byterange.length))
		} else {
			// Up to the beginning of the next excluded range
			hashRange(f, h, byterange.offset+byterange.length, ranges[i+1].offset-(byterange.offset+byterange.length))
		}
	}

	return h
}

func hashRange(f *os.File, h hash.Hash, offset int64, length int64) {
	fmt.Println("...continuing with checksum from offset", strconv.FormatInt(offset, 10), "for", strconv.FormatInt(length, 10), "bytes")
	s := io.NewSectionReader(f, offset, length)
	if _, err := io.Copy(h, s); err != nil {
		log.Fatal(err)
	}
}

func hashDummyRange(h hash.Hash, length int) {
	fmt.Println("Checksum for", strconv.Itoa(length), "bytes as if they were 0x00")
	h.Write(bytes.Repeat([]byte{0x00}, length))
}
$ xxd file.txt 
00000000: 3132 3334 3500 0000 0000 3132 3334 3500  12345.....12345.
00000010: 0000 0000 3132 3334 3536 3738 3930 3132  ....123456789012
00000020: 3334 3536 3738 3930 0a                   34567890.

Results in:

The file is 41 bytes long
...continuing with checksum from offset 0 for 5 bytes
range offset 5 length 5
Checksum for 5 bytes as if they were 0x00
...continuing with checksum from offset 10 for 5 bytes
range offset 15 length 5
Checksum for 5 bytes as if they were 0x00
...continuing with checksum from offset 20 for 21 bytes
8701e9551045bb079246c95e8cb86c56fb74f85ee52bb1605aae6d8a09e384a3
Compare to doing it in one go
8701e9551045bb079246c95e8cb86c56fb74f85ee52bb1605aae6d8a09e384a3

from go-appimage.

probonopd avatar probonopd commented on August 26, 2024

Note to self, sha256 digest calculation works now like TheAssassin's implementation (which is somewhat non-standard): e511ed7

from go-appimage.

probonopd avatar probonopd commented on August 26, 2024

Done

from go-appimage.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.