소스 검색

Get rid of ETag signature; Change ETag hash to SHA256; Add verison

DarthSim 6 년 전
부모
커밋
7f567216dd
3개의 변경된 파일14개의 추가작업 그리고 14개의 파일을 삭제
  1. 7 10
      config.go
  2. 5 4
      etag.go
  3. 2 0
      main.go

+ 7 - 10
config.go

@@ -2,7 +2,6 @@ package main
 
 import (
 	"bytes"
-	"crypto/rand"
 	"encoding/hex"
 	"flag"
 	"fmt"
@@ -101,8 +100,7 @@ type config struct {
 
 	LocalFileSystemRoot string
 
-	ETagEnabled   bool
-	ETagSignature []byte
+	ETagEnabled bool
 
 	BaseURL string
 }
@@ -124,8 +122,14 @@ var conf = config{
 func init() {
 	keypath := flag.String("keypath", "", "path of the file with hex-encoded key")
 	saltpath := flag.String("saltpath", "", "path of the file with hex-encoded salt")
+	showVersion := flag.Bool("v", false, "show version")
 	flag.Parse()
 
+	if *showVersion {
+		fmt.Println(version)
+		os.Exit(0)
+	}
+
 	if port := os.Getenv("PORT"); len(port) > 0 {
 		conf.Bind = fmt.Sprintf(":%s", port)
 	}
@@ -232,13 +236,6 @@ func init() {
 		}
 	}
 
-	if conf.ETagEnabled {
-		conf.ETagSignature = make([]byte, 16)
-		rand.Read(conf.ETagSignature)
-		log.Printf("ETag support is activated. The random value was generated to be used for ETag calculation: %s\n",
-			fmt.Sprintf("%x", conf.ETagSignature))
-	}
-
 	initVips()
 	initDownloading()
 }

+ 5 - 4
etag.go

@@ -1,7 +1,7 @@
 package main
 
 import (
-	"crypto/sha1"
+	"crypto/sha256"
 	"encoding/binary"
 	"fmt"
 )
@@ -9,12 +9,13 @@ import (
 var notModifiedErr = newError(304, "Not modified", "Not modified")
 
 func calcETag(b []byte, po *processingOptions) string {
-	footprint := sha1.Sum(b)
+	footprint := sha256.Sum256(b)
 
-	hash := sha1.New()
+	hash := sha256.New()
 	hash.Write(footprint[:])
+	hash.Write([]byte(version))
+	binary.Write(hash, binary.LittleEndian, conf)
 	binary.Write(hash, binary.LittleEndian, *po)
-	hash.Write(conf.ETagSignature)
 
 	return fmt.Sprintf("%x", hash.Sum(nil))
 }

+ 2 - 0
main.go

@@ -12,6 +12,8 @@ import (
 	"golang.org/x/net/netutil"
 )
 
+const version = "1.1.7"
+
 func main() {
 	// Force garbage collection
 	go func() {