Check-in [6dc823f396]

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Added crypto
Downloads: Tarball | ZIP archive
Timelines: family | ancestors | trunk
Files: files | file ages | folders
SHA1: 6dc823f396bbd539ffb641633341e7e761dfafef
User & Date: schollz 2016-11-17 18:39:49.389
Context
2016-11-17
18:39
Added crypto Leaf check-in: 6dc823f396 user: schollz tags: trunk
18:18
initial empty check-in check-in: f0209aca5a user: schollz tags: trunk
Changes
Unified Diff Ignore Whitespace Patch
Added src/crypto.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package main

import (
	"bytes"
	"compress/flate"
	"crypto/sha256"
	"encoding/hex"
	"fmt"
	"io"
	"io/ioutil"

	"github.com/gtank/cryptopasta"
)

var ENCRYPTION_COMPRESSION = true

func main() {
	test := `func decrypt(content, password []byte) ([]byte, error) {
		content = decompressByte(content)
		key := sha256.Sum256(password)
		decrypted, err := cryptopasta.Decrypt(content, &key)
		return decrypted, err
	}

	func encrypt(content, password []byte) ([]byte, error) {
		key := sha256.Sum256([]byte(password))
		encrypted, err := cryptopasta.Encrypt(content, &key)
		encrypted = compressByte(encrypted)
		return encrypted, err
	}

	func encryptString(content string, password string) (string, error) {
		bEncrypted, err := encrypt([]byte(content), []byte(password))
		if err != nil {
			return "", err
		}
		return hex.EncodeToString(bEncrypted), nil
	}`
	test = "test.txt.gz"
	a, _ := encryptString(test, "1234")
	fmt.Println(a)
	fmt.Println(len(a))
	fmt.Println(len(test))
	// fmt.Println(decryptString(a, "1234"))
	fmt.Println(encryptAndWrite("test.txt", "somedata", "1234"))
	fmt.Println(openAndDecrypt("test.txt", "124"))
}

func decrypt(content, password []byte) ([]byte, error) {
	key := sha256.Sum256(password)
	decrypted, err := cryptopasta.Decrypt(content, &key)
	if ENCRYPTION_COMPRESSION {
		decrypted = decompressByte(decrypted)
	}
	return decrypted, err
}

func encrypt(content, password []byte) ([]byte, error) {
	if ENCRYPTION_COMPRESSION {
		content = compressByte(content)
	}
	key := sha256.Sum256([]byte(password))
	encrypted, err := cryptopasta.Encrypt(content, &key)
	return encrypted, err
}

func encryptString(content string, password string) (string, error) {
	bEncrypted, err := encrypt([]byte(content), []byte(password))
	if err != nil {
		return "", err
	}
	return hex.EncodeToString(bEncrypted), nil
}

func decryptString(encodedContent string, password string) (string, error) {
	bEncrypted, err := hex.DecodeString(encodedContent)
	if err != nil {
		return "", err
	}
	bDecrypt, err := decrypt(bEncrypted, []byte(password))
	return string(bDecrypt), err
}

func encryptAndWrite(filename, content, password string) (err error) {
	e, err := encrypt([]byte(content), []byte(password))
	if err != nil {
		return err
	}
	return ioutil.WriteFile(filename, e, 0644)
}

func openAndDecrypt(filename, password string) (string, error) {
	contentBytes, err := ioutil.ReadFile(filename)
	if err != nil {
		return "", err
	}
	decryptedContent, err := decrypt(contentBytes, []byte(password))
	return string(decryptedContent), err
}

// compressByte returns a compressed byte slice.
func compressByte(src []byte) []byte {
	compressedData := new(bytes.Buffer)
	compress(src, compressedData, 9)
	return compressedData.Bytes()
}

// decompressByte returns a decompressed byte slice.
func decompressByte(src []byte) []byte {
	compressedData := bytes.NewBuffer(src)
	deCompressedData := new(bytes.Buffer)
	decompress(compressedData, deCompressedData)
	return deCompressedData.Bytes()
}

// compress uses flate to compress a byte slice to a corresponding level
func compress(src []byte, dest io.Writer, level int) {
	compressor, _ := flate.NewWriter(dest, level)
	compressor.Write(src)
	compressor.Close()
}

// compress uses flate to decompress an io.Reader
func decompress(src io.Reader, dest io.Writer) {
	decompressor := flate.NewReader(src)
	io.Copy(dest, decompressor)
	decompressor.Close()
}