forked from Sydsvenskan/xferspdy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fpgen.go
64 lines (51 loc) · 1.66 KB
/
fpgen.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// Copyright 2015 Monmohan Singh. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"encoding/gob"
"flag"
"fmt"
"github.com/golang/glog"
"github.com/monmohan/xferspdy"
"os"
"path/filepath"
)
var (
fPath = flag.String("file", "", "File path to create the fingerprint, REQUIRED ")
blockSz = flag.Uint64("blocksz", 2*1024, "Block Size, default block size is 2KB")
verify = flag.Bool("verify", false, "Verify fingerprint on creation")
)
func main() {
flag.Parse()
if *fPath == "" {
fmt.Println("Missing File parameter")
flag.Usage()
return
}
glog.V(2).Infof("File path %s , Block Size %d \n", *fPath, *blockSz)
fgprt := xferspdy.NewFingerprint(*fPath, uint32(*blockSz))
glog.V(4).Infof("Signature %s \n", *fgprt)
dir, fname := filepath.Split(*fPath)
fname = filepath.Join(dir, fname+".fingerprint")
fpfile, err := os.OpenFile(fname, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0777)
if err != nil {
glog.Fatalf("Error in creating finger print file %v \n, Error :%s", filepath.Join(dir, fname+".fingerprint"), err)
}
enc := gob.NewEncoder(fpfile)
enc.Encode(*fgprt)
fmt.Printf("Fingerprint for file: %v \nGenerated: %v \n ", *fPath, fpfile.Name())
fpfile.Close()
fpfile, err = os.Open(fname)
defer fpfile.Close()
var fp xferspdy.Fingerprint
dec := gob.NewDecoder(fpfile)
err = dec.Decode(&fp)
if *verify {
glog.V(4).Infof("Verifying signature , created %v\n decoded from file %v\n", *fgprt, fp)
if err != nil || (len(fgprt.BlockMap) != len(fp.BlockMap)) {
glog.Fatalf("Failed to decode finger print during verification %v\n", err)
}
}
glog.Flush()
}