first commit
This commit is contained in:
commit
521d7bead4
61
.gitignore
vendored
Normal file
61
.gitignore
vendored
Normal file
|
@ -0,0 +1,61 @@
|
|||
# Created by .ignore support plugin (hsz.mobi)
|
||||
### Go template
|
||||
# Binaries for programs and plugins
|
||||
*.exe
|
||||
*.dll
|
||||
*.so
|
||||
*.dylib
|
||||
|
||||
# Test binary, build with `go test -c`
|
||||
*.test
|
||||
|
||||
# Output of the go coverage tool, specifically when used with LiteIDE
|
||||
*.out
|
||||
|
||||
# Project-local glide cache, RE: https://github.com/Masterminds/glide/issues/736
|
||||
.glide/
|
||||
### JetBrains template
|
||||
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and Webstorm
|
||||
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839
|
||||
|
||||
# User-specific stuff:
|
||||
.idea/**/workspace.xml
|
||||
.idea/**/tasks.xml
|
||||
.idea/dictionaries
|
||||
|
||||
# Sensitive or high-churn files:
|
||||
.idea/**/dataSources/
|
||||
.idea/**/dataSources.ids
|
||||
.idea/**/dataSources.xml
|
||||
.idea/**/dataSources.local.xml
|
||||
.idea/**/sqlDataSources.xml
|
||||
.idea/**/dynamic.xml
|
||||
.idea/**/uiDesigner.xml
|
||||
|
||||
# Gradle:
|
||||
.idea/**/gradle.xml
|
||||
.idea/**/libraries
|
||||
|
||||
# Mongo Explorer plugin:
|
||||
.idea/**/mongoSettings.xml
|
||||
|
||||
## File-based project format:
|
||||
*.iws
|
||||
|
||||
## Plugin-specific files:
|
||||
|
||||
# IntelliJ
|
||||
/out/
|
||||
|
||||
# mpeltonen/sbt-idea plugin
|
||||
.idea_modules/
|
||||
|
||||
# JIRA plugin
|
||||
atlassian-ide-plugin.xml
|
||||
|
||||
# Crashlytics plugin (for Android Studio and IntelliJ)
|
||||
com_crashlytics_export_strings.xml
|
||||
crashlytics.properties
|
||||
crashlytics-build.properties
|
||||
fabric.properties
|
||||
|
71
file_cipher.go
Normal file
71
file_cipher.go
Normal file
|
@ -0,0 +1,71 @@
|
|||
package file_cipher_go
|
||||
|
||||
import (
|
||||
"crypto/aes"
|
||||
"crypto/cipher"
|
||||
"os"
|
||||
"io"
|
||||
)
|
||||
|
||||
var KEY = []byte("qwerqwerqwerqwer")
|
||||
|
||||
|
||||
func Encrypt(filePath, destName string) string {
|
||||
|
||||
inFile, err := os.Open(filePath)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
defer inFile.Close()
|
||||
|
||||
block, err := aes.NewCipher(KEY)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
var iv [aes.BlockSize]byte
|
||||
stream := cipher.NewOFB(block, iv[:])
|
||||
|
||||
outFile, err := os.OpenFile(destName, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
defer outFile.Close()
|
||||
|
||||
|
||||
writer := &cipher.StreamWriter{S: stream, W: outFile}
|
||||
if _, err := io.Copy(writer, inFile); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
return outFile.Name()
|
||||
}
|
||||
|
||||
func Decrypt(filePath, destName string) string {
|
||||
inFile, err := os.Open(filePath)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
defer inFile.Close()
|
||||
|
||||
block, err := aes.NewCipher(KEY)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
var iv [aes.BlockSize]byte
|
||||
stream := cipher.NewOFB(block, iv[:])
|
||||
|
||||
outFile, err := os.OpenFile(destName, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
defer outFile.Close()
|
||||
|
||||
reader := &cipher.StreamReader{S: stream, R: inFile}
|
||||
if _, err := io.Copy(outFile, reader); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
return outFile.Name()
|
||||
}
|
11
file_cipher_test.go
Normal file
11
file_cipher_test.go
Normal file
|
@ -0,0 +1,11 @@
|
|||
package file_cipher_go
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestEncrypt(t *testing.T) {
|
||||
Encrypt("/home/insanity/addressbook.proto", "/home/insanity/addressbook.enc")
|
||||
}
|
||||
|
||||
func TestDecrypt(t *testing.T) {
|
||||
Decrypt("/home/insanity/addressbook.enc", "/home/insanity/addressbook.dec")
|
||||
}
|
Loading…
Reference in New Issue
Block a user