Denmaseno 8 gadi atpakaļ
vecāks
revīzija
58593cd63e
5 mainītis faili ar 253 papildinājumiem un 180 dzēšanām
  1. 59 0
      crypt-aes.go
  2. 116 0
      crypt-rsa.go
  3. 1 1
      file.go
  4. 62 0
      part-writer-data.go
  5. 15 179
      util.go

+ 59 - 0
crypt-aes.go

@@ -0,0 +1,59 @@
+package util
+
+import (
+	"crypto/aes"
+	"crypto/cipher"
+	"crypto/rand"
+	"fmt"
+	"io"
+)
+
+// KeyAES func
+func KeyAES() []byte {
+	var err error
+	var key = make([]byte, 32)
+	if _, err = rand.Read(key); err != nil {
+		panic(fmt.Errorf("Error generate aes key\n\n%+v", err))
+	}
+	return key
+}
+
+// EncryptAES func
+func EncryptAES(key, data []byte) []byte {
+	block, err := aes.NewCipher(key)
+	if err != nil {
+		panic(err)
+	}
+
+	ciphertext := make([]byte, aes.BlockSize+len(data))
+	iv := ciphertext[:aes.BlockSize]
+	if _, err := io.ReadFull(rand.Reader, iv); err != nil {
+		panic(err)
+	}
+
+	stream := cipher.NewCFBEncrypter(block, iv)
+	stream.XORKeyStream(ciphertext[aes.BlockSize:], data)
+
+	return ciphertext
+}
+
+// DecryptAES func
+func DecryptAES(key, ciphertext []byte) []byte {
+	block, err := aes.NewCipher(key)
+	if err != nil {
+		panic(fmt.Errorf("Error creating new block cipher\n%v\n", err))
+	}
+
+	if len(ciphertext) < aes.BlockSize {
+		panic(fmt.Errorf("ciphertext too short"))
+	}
+	iv := ciphertext[:aes.BlockSize]
+	ciphertext = ciphertext[aes.BlockSize:]
+
+	stream := cipher.NewCFBDecrypter(block, iv)
+
+	// XORKeyStream can work in-place if the two arguments are the same.
+	stream.XORKeyStream(ciphertext, ciphertext)
+
+	return ciphertext
+}

+ 116 - 0
crypt-rsa.go

@@ -0,0 +1,116 @@
+package util
+
+import (
+	"bytes"
+	"crypto/rand"
+	"crypto/rsa"
+	"crypto/x509"
+)
+
+// KeyRSA func
+func KeyRSA() *rsa.PrivateKey {
+	var privateKey *rsa.PrivateKey
+	var err error
+
+	if privateKey, err = rsa.GenerateKey(rand.Reader, 2048); err != nil {
+		panic(err)
+	}
+
+	return privateKey
+}
+
+// MarshalPublicKey func
+func MarshalPublicKey(key *rsa.PublicKey) []byte {
+	if data, err := x509.MarshalPKIXPublicKey(key); err != nil {
+		panic(err)
+	} else {
+		return data
+	}
+}
+
+// UnmarshalPublicKey func
+func UnmarshalPublicKey(key []byte) *rsa.PublicKey {
+	var kk interface{}
+	var err error
+	if kk, err = x509.ParsePKIXPublicKey(key); err != nil {
+		panic(err)
+	}
+	return kk.(*rsa.PublicKey)
+}
+
+// MarshalPrivateKey func
+func MarshalPrivateKey(key *rsa.PrivateKey) []byte {
+	return x509.MarshalPKCS1PrivateKey(key)
+}
+
+// UnmarshalPrivateKey func
+func UnmarshalPrivateKey(key []byte) *rsa.PrivateKey {
+	var kk *rsa.PrivateKey
+	var err error
+	if kk, err = x509.ParsePKCS1PrivateKey(key); err != nil {
+		panic(err)
+	}
+	return kk
+}
+
+// EncryptRSA func
+func EncryptRSA(key *rsa.PublicKey, data []byte) []byte {
+	var err error
+	klen := key.N.BitLen()/8 - 11
+	if len(data) <= klen {
+		var bb []byte
+		if bb, err = rsa.EncryptPKCS1v15(rand.Reader, key, data); err != nil {
+			panic(err)
+		}
+		return bb
+	}
+	var buf bytes.Buffer
+	var bb []byte
+	for i, w, r := 0, 0, len(data); r > 0; i, r = i+w, r-w {
+		if r <= klen {
+			if bb, err = rsa.EncryptPKCS1v15(rand.Reader, key, data[i:]); err != nil {
+				panic(err)
+			}
+			buf.Write(bb)
+			w = r
+		} else {
+			if bb, err = rsa.EncryptPKCS1v15(rand.Reader, key, data[i:i+klen]); err != nil {
+				panic(err)
+			}
+			buf.Write(bb)
+			w = klen
+		}
+	}
+	return buf.Bytes()
+}
+
+// DecryptRSA func
+func DecryptRSA(key *rsa.PrivateKey, data []byte) []byte {
+	var err error
+	klen := key.N.BitLen() / 8
+	if len(data) <= klen {
+		var bb []byte
+		if bb, err = rsa.DecryptPKCS1v15(rand.Reader, key, data); err != nil {
+			panic(err)
+		}
+		return bb
+	}
+	var buf bytes.Buffer
+	var bb []byte
+	for i, w, r := 0, 0, len(data); r > 0; i, r = i+w, r-w {
+		if r <= klen {
+			if bb, err = rsa.DecryptPKCS1v15(rand.Reader, key, data[i:]); err != nil {
+				panic(err)
+			}
+			buf.Write(bb)
+			w = r
+		} else {
+			if bb, err = rsa.DecryptPKCS1v15(rand.Reader, key, data[i:i+klen]); err != nil {
+				panic(err)
+			}
+			buf.Write(bb)
+			w = klen
+		}
+	}
+	return buf.Bytes()
+}

+ 1 - 1
file.go

@@ -7,6 +7,6 @@ func RemoveAll(path ...string) {
 	var err error
 	for _, p := range path {
 		err = os.RemoveAll(p)
-		Check(err, "Remove error: path '%s'\n%v", p)
+		Check("Remove error: path '%s'\n%v", p, err)
 	}
 }

+ 62 - 0
part-writer-data.go

@@ -0,0 +1,62 @@
+package util
+
+import (
+	"os"
+	"strconv"
+)
+
+// PartWriter interface
+type PartWriter interface {
+	Next()
+
+	Write(p []byte) (n int, err error)
+
+	Close() error
+
+	Part() uint64
+}
+
+// PartWriterData struct
+type PartWriterData struct {
+	fn   string
+	part uint64
+	file *os.File
+}
+
+// NewPartWriter func
+func NewPartWriter(path string, id uint64) *PartWriterData {
+	var err error
+	var file *os.File
+	fn := path + "/F" + strconv.FormatUint(id, 16) + "."
+	if file, err = os.Create(fn + strconv.FormatUint(uint64(1), 16) + ".tmp"); err != nil {
+		panic(err)
+	}
+	return &PartWriterData{fn, 1, file}
+}
+
+// Next func
+func (w *PartWriterData) Next() {
+	var err error
+	if err = w.file.Close(); err != nil {
+		panic(err)
+	}
+	w.part++
+	if w.file, err = os.Create(w.fn + strconv.FormatUint(uint64(w.part), 16) + ".tmp"); err != nil {
+		panic(err)
+	}
+}
+
+// Write func
+func (w *PartWriterData) Write(p []byte) (n int, err error) {
+	return w.file.Write(p)
+}
+
+// Close func
+func (w *PartWriterData) Close() error {
+	return w.file.Close()
+}
+
+// Part func
+func (w *PartWriterData) Part() uint64 {
+	return w.part
+}

+ 15 - 179
util.go

@@ -1,12 +1,7 @@
 package util
 
 import (
-	"bytes"
-	"crypto/aes"
-	"crypto/cipher"
-	"crypto/rand"
 	"crypto/rsa"
-	"crypto/x509"
 	"encoding/binary"
 	"encoding/json"
 	"fmt"
@@ -19,16 +14,17 @@ type Signer interface {
 }
 
 // Check error function
-func Check(err error, args ...interface{}) {
-	if err != nil {
-		if len(args) == 0 {
-			panic(err)
-		} else {
-			format := args[0].(string)
-			args[0] = err
-			panic(fmt.Errorf(format, append(args[1:], err)...))
+func Check(format string, args ...interface{}) {
+	var hasError bool
+	for _, v := range args {
+		if err, ok := v.(error); ok && err != nil {
+			hasError = true
+			break
 		}
 	}
+	if hasError {
+		panic(fmt.Errorf(format, args...))
+	}
 }
 
 // Uint64ToBytes func
@@ -45,52 +41,6 @@ func UintToBytes(v uint) []byte {
 	return bb
 }
 
-// KeyRSA func
-func KeyRSA() *rsa.PrivateKey {
-	var privateKey *rsa.PrivateKey
-	var err error
-
-	if privateKey, err = rsa.GenerateKey(rand.Reader, 2048); err != nil {
-		panic(err)
-	}
-
-	return privateKey
-}
-
-// MarshalPublicKey func
-func MarshalPublicKey(key *rsa.PublicKey) []byte {
-	if data, err := x509.MarshalPKIXPublicKey(key); err != nil {
-		panic(err)
-	} else {
-		return data
-	}
-}
-
-// UnmarshalPublicKey func
-func UnmarshalPublicKey(key []byte) *rsa.PublicKey {
-	var kk interface{}
-	var err error
-	if kk, err = x509.ParsePKIXPublicKey(key); err != nil {
-		panic(err)
-	}
-	return kk.(*rsa.PublicKey)
-}
-
-// MarshalPrivateKey func
-func MarshalPrivateKey(key *rsa.PrivateKey) []byte {
-	return x509.MarshalPKCS1PrivateKey(key)
-}
-
-// UnmarshalPrivateKey func
-func UnmarshalPrivateKey(key []byte) *rsa.PrivateKey {
-	var kk *rsa.PrivateKey
-	var err error
-	if kk, err = x509.ParsePKCS1PrivateKey(key); err != nil {
-		panic(err)
-	}
-	return kk
-}
-
 // JSONMarshal func
 func JSONMarshal(w io.Writer, v interface{}) {
 	if d, err := json.Marshal(v); err == nil {
@@ -108,126 +58,12 @@ func JSONUnmarshal(b []byte, v interface{}) interface{} {
 	return v
 }
 
-// PrivateKey func
-func PrivateKey() *rsa.PrivateKey {
-	var err error
-	var privateKey *rsa.PrivateKey
-
-	if privateKey, err = rsa.GenerateKey(rand.Reader, 2048); err != nil {
-		panic(err)
-	}
-
-	return privateKey
-}
-
-// EncryptRSA func
-func EncryptRSA(key *rsa.PublicKey, data []byte) []byte {
-	var err error
-	klen := key.N.BitLen()/8 - 11
-	if len(data) <= klen {
-		var bb []byte
-		if bb, err = rsa.EncryptPKCS1v15(rand.Reader, key, data); err != nil {
-			panic(err)
+// InSlice func
+func InSlice(v interface{}, list ...interface{}) bool {
+	for _, vl := range list {
+		if v == vl {
+			return true
 		}
-		return bb
 	}
-	var buf bytes.Buffer
-	var bb []byte
-	for i, w, r := 0, 0, len(data); r > 0; i, r = i+w, r-w {
-		if r <= klen {
-			if bb, err = rsa.EncryptPKCS1v15(rand.Reader, key, data[i:]); err != nil {
-				panic(err)
-			}
-			buf.Write(bb)
-			w = r
-		} else {
-			if bb, err = rsa.EncryptPKCS1v15(rand.Reader, key, data[i:i+klen]); err != nil {
-				panic(err)
-			}
-			buf.Write(bb)
-			w = klen
-		}
-	}
-	return buf.Bytes()
-}
-
-// DecryptRSA func
-func DecryptRSA(key *rsa.PrivateKey, data []byte) []byte {
-	var err error
-	klen := key.N.BitLen() / 8
-	if len(data) <= klen {
-		var bb []byte
-		if bb, err = rsa.DecryptPKCS1v15(rand.Reader, key, data); err != nil {
-			panic(err)
-		}
-		return bb
-	}
-	var buf bytes.Buffer
-	var bb []byte
-	for i, w, r := 0, 0, len(data); r > 0; i, r = i+w, r-w {
-		if r <= klen {
-			if bb, err = rsa.DecryptPKCS1v15(rand.Reader, key, data[i:]); err != nil {
-				panic(err)
-			}
-			buf.Write(bb)
-			w = r
-		} else {
-			if bb, err = rsa.DecryptPKCS1v15(rand.Reader, key, data[i:i+klen]); err != nil {
-				panic(err)
-			}
-			buf.Write(bb)
-			w = klen
-		}
-	}
-	return buf.Bytes()
-}
-
-// KeyAES func
-func KeyAES() []byte {
-	var err error
-	var key = make([]byte, 32)
-	if _, err = rand.Read(key); err != nil {
-		panic(fmt.Errorf("Error generate aes key\n\n%+v", err))
-	}
-	return key
-}
-
-// EncryptAES func
-func EncryptAES(key, data []byte) []byte {
-	block, err := aes.NewCipher(key)
-	if err != nil {
-		panic(err)
-	}
-
-	ciphertext := make([]byte, aes.BlockSize+len(data))
-	iv := ciphertext[:aes.BlockSize]
-	if _, err := io.ReadFull(rand.Reader, iv); err != nil {
-		panic(err)
-	}
-
-	stream := cipher.NewCFBEncrypter(block, iv)
-	stream.XORKeyStream(ciphertext[aes.BlockSize:], data)
-
-	return ciphertext
-}
-
-// DecryptAES func
-func DecryptAES(key, ciphertext []byte) []byte {
-	block, err := aes.NewCipher(key)
-	if err != nil {
-		panic(fmt.Errorf("Error creating new block cipher\n%v\n", err))
-	}
-
-	if len(ciphertext) < aes.BlockSize {
-		panic(fmt.Errorf("ciphertext too short"))
-	}
-	iv := ciphertext[:aes.BlockSize]
-	ciphertext = ciphertext[aes.BlockSize:]
-
-	stream := cipher.NewCFBDecrypter(block, iv)
-
-	// XORKeyStream can work in-place if the two arguments are the same.
-	stream.XORKeyStream(ciphertext, ciphertext)
-
-	return ciphertext
+	return false
 }