Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

finished basic multicore checksum + encryption

  • Loading branch information...
commit ed2b92a0264f89291ca66f7041c68eec4703deb1 1 parent daec755
@spikebike authored
Showing with 52 additions and 34 deletions.
  1. +52 −34 src/bdrupload/bdrupload.go
View
86 src/bdrupload/bdrupload.go
@@ -1,49 +1,67 @@
package bdrupload
import (
- "os"
- "fmt"
- "time"
"bufio"
+ "crypto/aes"
+ "crypto/cipher"
"crypto/sha256"
+ "fmt"
+ "os"
+ "time"
)
type Upchan_t struct {
- Rowid int64
- Path string
+ Rowid int64
+ Path string
}
type Downchan_t struct {
- Rowid int
- Err error
+ Rowid int
+ Err error
}
+var commonIV = []byte{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}
+
+const bufferSize = 524288
+
func Server(upchan chan *Upchan_t, done chan bool) {
- var count int
- var size int64
- buffer := make([]byte, 16384)
-
- for f := range upchan {
- fmt.Printf("Server: received rowID=%d path=%s\n", f.Rowid, f.Path)
- // fmt.Printf("%T %#v\n",f,f)
- size = 0
- file, err := os.Open(f.Path)
- t0 := time.Now().UnixNano()
- reader := bufio.NewReader(file)
- h := sha256.New() // h is a hash.Hash
- for {
- if count, err = reader.Read(buffer); err != nil {
- break
- }
- size=size+int64(count)
- h.Write(buffer[:count])
- }
- t1 := time.Now().UnixNano()
- seconds :=float64(t1-t0)/1000000000
- fmt.Printf("%x %s %4.2f MB/sec\n", h.Sum(nil), f.Path,float64(size)/(1024*1024*seconds))
-
- }
- fmt.Print("Server: Channel closed, existing\n")
- done <- true
-}
+ var count int
+ var size int64
+ readBuffer := make([]byte, bufferSize)
+ ciphertext := make([]byte, bufferSize)
+ key_text := "32o4908go293hohg98fh40ghaidlkjk1"
+ c, err := aes.NewCipher([]byte(key_text))
+ if err != nil {
+ fmt.Printf("Error: NewCipher(%d bytes) = %s", len(key_text), err)
+ os.Exit(-1)
+ }
+ for f := range upchan {
+ fmt.Printf("Server: received rowID=%d path=%s\n", f.Rowid, f.Path)
+ // fmt.Printf("%T %#v\n",f,f)
+ size = 0
+
+ // open file and create a reader
+ file, err := os.Open(f.Path)
+ reader := bufio.NewReader(file)
+ // for this file create a cipher and new sha256 state
+ cfb := cipher.NewCFBEncrypter(c, commonIV)
+ h := sha256.New() // h is a hash.Hash
+
+ t0 := time.Now().UnixNano()
+ for {
+ if count, err = reader.Read(readBuffer); err != nil {
+ break
+ }
+ size = size + int64(count)
+ cfb.XORKeyStream(ciphertext[:count], readBuffer[:count])
+ h.Write(ciphertext[:count])
+ }
+ t1 := time.Now().UnixNano()
+ seconds := float64(t1-t0) / 1000000000
+ fmt.Printf("%x %s %4.2f MB/sec\n", h.Sum(nil), f.Path, float64(size)/(1024*1024*seconds))
+
+ }
+ fmt.Print("Server: Channel closed, existing\n")
+ done <- true
+}
Please sign in to comment.
Something went wrong with that request. Please try again.