Update Picocrypt.go

This commit is contained in:
Evan Su 2021-05-03 12:48:39 -04:00 committed by GitHub
parent 7f141e6066
commit 0ebf21aa01
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 495 additions and 196 deletions

View File

@ -18,17 +18,26 @@ import (
"time"
"strings"
"strconv"
"image/color"
//"image/color"
"crypto/md5"
"crypto/rand"
"crypto/sha1"
"encoding/hex"
"path/filepath"
"crypto/sha256"
"github.com/pkg/browser"
"github.com/zeebo/blake3"
"golang.org/x/crypto/sha3"
"golang.org/x/crypto/argon2"
"golang.org/x/crypto/blake2b"
g "github.com/AllenDang/giu"
di "github.com/sqweek/dialog"
ig "github.com/AllenDang/imgui-go"
"golang.org/x/crypto/blake2b"
"golang.org/x/crypto/blake2s"
"github.com/atotto/clipboard"
//"github.com/AllenDang/imgui-go"
"github.com/klauspost/reedsolomon"
"golang.org/x/crypto/chacha20poly1305"
"github.com/HACKERALERT/Monocypher-Go/monocypher"
)
// Global variables
@ -42,10 +51,13 @@ var inputFile string
var outputFile string
// UI-related global variables
var tab = 0
var inputLabel = "Drag and drop file(s) and folder(s) into this window."
var outputEntry string
var outputWidth float32 = 376
var orLabel = "or"
var passwordState = g.InputTextFlags_Password
var passwordToggleString = "Show"
var progress float32 = 0
var progressInfo = ""
var status = "Ready."
@ -57,6 +69,9 @@ var metadata string
var keep bool
var erase bool
var reedsolo bool
var split bool
var splitSize string
var fast bool
// Reed-Solomon encoders
var rs5_128,_ = reedsolomon.New(5,128)
@ -66,18 +81,37 @@ var rs24_128,_ = reedsolomon.New(24,128)
var rs32_128,_ = reedsolomon.New(32,128)
var rs64_128,_ = reedsolomon.New(64,128)
// File checksum generator variables
var cs_md5 string
var cs_sha1 string
var cs_sha256 string
var cs_sha3_256 string
var cs_blake2b string
var cs_blake2s string
var cs_blake3 string
var cs_progress float32 = 0
var md5_selected = false
var sha1_selected = false
var sha256_selected = false
var sha3_256_selected = false
var blake2b_selected = false
var blake2s_selected = false
var blake3_selected = false
// Create the user interface
func startUI(){
g.SingleWindow("Picocrypt").Layout(
// Some styling for aesthetics
g.Style().SetColor(
ig.StyleColorBorder,
color.RGBA{0x06,0x34,0x55,255},
).To(
// The tab bar, which contains different tabs for different features
g.TabBar("TabBar").Layout(
// File encryption/decryption tab
g.TabItem("Encryption/decryption").Layout(
// Update 'tab' to indicate active tab
g.Custom(func(){
if g.IsItemActive(){
tab = 0
}
}),
// Label listing the input files and button to clear input files
g.Dummy(30,0),
g.Line(
@ -110,12 +144,24 @@ func startUI(){
// Prompt for password
g.Dummy(10,0),
g.Label("Password:"),
g.InputText("##password",&password).Size(200/dpi),
g.Line(
g.InputText("##password",&password).Size(200/dpi).Flags(passwordState),
g.Button(passwordToggleString).OnClick(func(){
if passwordState==g.InputTextFlags_Password{
passwordState = g.InputTextFlags_None
passwordToggleString = "Hide"
}else{
passwordState = g.InputTextFlags_Password
passwordToggleString = "Show"
}
g.Update()
}),
),
// Prompt to confirm password
g.Dummy(10,0),
g.Label("Confirm password:"),
g.InputText("##cPassword",&cPassword).Size(200/dpi),
g.InputText("##cPassword",&cPassword).Size(200/dpi).Flags(passwordState),
// Optional metadata
g.Dummy(10,0),
@ -126,16 +172,163 @@ func startUI(){
g.Dummy(10,0),
g.Checkbox("Keep decrypted output even if it's corrupted or modified",&keep),
g.Checkbox("Securely erase and delete original file(s)",&erase),
g.Line(
g.Checkbox("Encode with Reed-Solomon to prevent corruption",&reedsolo),
g.Button("?").OnClick(func(){
browser.OpenURL("https://en.wikipedia.org/wiki/Reed%E2%80%93Solomon_error_correction")
}),
),
g.Line(
g.Checkbox("Split output into chunks of",&split),
g.InputText("##splitSize",&splitSize).Size(30).Flags(g.InputTextFlags_CharsDecimal),
g.Label("MB"),
),
g.Checkbox("Fast mode (less secure, not as durable)",&fast),
// Start and cancel buttons
g.Dummy(10,0),
g.Line(
g.Button("Start").Size(360,20).OnClick(func(){
g.Button("Start").Size(-1,20).OnClick(func(){
go work()
}),
g.Button("Cancel").Size(95,20),
/*// Progress bar
g.ProgressBar(progress).Size(-1,0).Overlay(progressInfo),
// Status label
g.Dummy(10,0),
g.Label(status),*/
// Credits and version
g.Line(
g.Label("Created by Evan Su. See the About tab for more info."),
g.Dummy(46,0),
g.Label("v1.13"),
),
),
// File shredder tab
g.TabItem("Shredder").Layout(
// Update 'tab' to indicate active tab
g.Custom(func(){
if g.IsItemActive(){
tab = 1
}
}),
),
// File checksum generator tab
g.TabItem("Checksum generator").Layout(
// Update 'tab' to indicate active tab
g.Custom(func(){
if g.IsItemActive(){
tab = 2
}
}),
g.Dummy(30,0),
g.Label("Toggle the hashes you would like to generate and drop a file here."),
// MD5
g.Dummy(10,0),
g.Line(
g.Checkbox("MD5:",&md5_selected),
g.Dummy(360,0),
g.Button("Copy").OnClick(func(){
clipboard.WriteAll(cs_md5)
}),
),
g.InputText("##cs_md5",&cs_md5).Size(-1).Flags(g.InputTextFlags_ReadOnly),
// SHA1
g.Dummy(10,0),
g.Line(
g.Checkbox("SHA1:",&sha1_selected),
g.Dummy(353,0),
g.Button("Copy").OnClick(func(){
clipboard.WriteAll(cs_sha1)
}),
),
g.InputText("##cs_sha1",&cs_sha1).Size(-1).Flags(g.InputTextFlags_ReadOnly),
// SHA256
g.Dummy(10,0),
g.Line(
g.Checkbox("SHA256:",&sha256_selected),
g.Dummy(341,0),
g.Button("Copy").OnClick(func(){
clipboard.WriteAll(cs_sha256)
}),
),
g.InputText("##cs_sha256",&cs_sha256).Size(-1).Flags(g.InputTextFlags_ReadOnly),
// SHA3-256
g.Dummy(10,0),
g.Line(
g.Checkbox("SHA3-256:",&sha3_256_selected),
g.Dummy(334,0),
g.Button("Copy").OnClick(func(){
clipboard.WriteAll(cs_sha3_256)
}),
),
g.InputText("##cs_sha3_256",&cs_sha3_256).Size(-1).Flags(g.InputTextFlags_ReadOnly),
// BLAKE2b
g.Dummy(10,0),
g.Line(
g.Checkbox("BLAKE2b:",&blake2b_selected),
g.Dummy(320,0),
g.Button("Copy").OnClick(func(){
clipboard.WriteAll(cs_blake2b)
}),
),
g.InputText("##cs_blake2b",&cs_blake2b).Size(-1).Flags(g.InputTextFlags_ReadOnly),
// BLAKE2s
g.Dummy(10,0),
g.Line(
g.Checkbox("BLAKE2s:",&blake2s_selected),
g.Dummy(320,0),
g.Button("Copy").OnClick(func(){
clipboard.WriteAll(cs_blake2s)
}),
),
g.InputText("##cs_blake2s",&cs_blake2s).Size(-1).Flags(g.InputTextFlags_ReadOnly),
// BLAKE3
g.Dummy(10,0),
g.Line(
g.Checkbox("BLAKE3:",&blake3_selected),
g.Dummy(323,0),
g.Button("Copy").OnClick(func(){
clipboard.WriteAll(cs_blake3)
}),
),
g.InputText("##cs_blake3",&cs_blake3).Size(-1).Flags(g.InputTextFlags_ReadOnly),
// Progress bar
g.Dummy(10,0),
g.Label("Progress:"),
g.ProgressBar(cs_progress).Size(-1,0),
),
g.TabItem("About").Layout(
// Update 'tab' to indicate active tab
g.Custom(func(){
if g.IsItemActive(){
tab = 3
}
}),
g.Dummy(30,0),
g.Label("Picocrypt, created by Evan Su (https://evansu.cc)"),
),
),
)
if working{
g.SingleWindow("Working..").IsOpen(&working).Layout(
g.Dummy(30,0),
g.Label("Tips:"),
g.Label(" - Choose a strong password with more than 16 characters."),
g.Label(" - Use a unique password that isn't used anywhere else."),
// Progress bar
g.ProgressBar(progress).Size(-1,0).Overlay(progressInfo),
@ -144,29 +337,16 @@ func startUI(){
g.Dummy(10,0),
g.Label(status),
// Credits and version
g.Line(
g.Label("Created by Evan Su."),
g.Label("v1.13"),
),
),
// File shredder tab
g.TabItem("Shredder").Layout(
),
// File checksum generator tab
g.TabItem("Checksum generator").Layout(
),
),
),
g.Button("Cancel").Size(95,20).OnClick(func(){
working = false
}),
)
}
}
// Handle files dropped into Picocrypt by user
func onDrop(names []string){
if tab==0{
// Clear variables
onlyFiles = nil
onlyFolders = nil
@ -232,9 +412,6 @@ func onDrop(names []string){
// Add the file
onlyFiles = append(onlyFiles,names[0])
// Set the file as 'outputEntry'
outputEntry = names[0]
inputFile = names[0]
}
}else{
@ -286,6 +463,11 @@ func onDrop(names []string){
})
}
}
}else if tab==1{
}else if tab==2{
go generateChecksums(names[0])
}
// Update the UI
g.Update()
@ -367,7 +549,7 @@ func work(){
// Write placeholder for hash of key
fout.Write(make([]byte,192))
// Write placeholder for Blake2 CRC
// Write placeholder for Blake3 CRC
fout.Write(make([]byte,160))
@ -410,7 +592,7 @@ func work(){
fin.Read(crcHash)
crcHash = rsDecode(crcHash,rs32_128,32)
_tmp := math.Ceil(float64(total-int64(metadataLength+919))/float64(1048744))
_tmp := math.Ceil(float64(total-int64(metadataLength+1063))/float64(1048744))
nonces = make([]byte,int(_tmp*152)+144)
fin.Read(nonces)
@ -448,7 +630,7 @@ func work(){
defer fout.Close()
}
crc,_ := blake2b.New256(nil)
crc := blake3.New()
done := 0
counter := 0
@ -482,6 +664,9 @@ func work(){
//fmt.Println("UNENCRYPTED NONCES: ",nonces)
}
for{
if !working{
return
}
//fmt.Println("Encrypt/decrypt loop")
var _data []byte
var data []byte
@ -512,9 +697,16 @@ func work(){
//fmt.Println("Data nonce: ",_nonce)
//fmt.Println("Data: ",data)
if mode=="encrypt"{
if fast{
data = cipher.Seal(nil,_nonce,data,nil)
crc.Write(data)
fout.Write(data)
}else{
mac,data := monocypher.Lock(data,_nonce,key)
fout.Write(data)
fout.Write(mac)
}
crc.Write(data)
//fout.Write(data)
}else{
//fmt.Println("DECODE LOOP")
crc.Write(data)
@ -526,6 +718,7 @@ func work(){
done += 1048576
counter++
progress = float32(done)/float32(total)
elapsed:= float64(int64(time.Now().Sub(startTime)))/float64(1000000000)
@ -538,7 +731,6 @@ func work(){
status = fmt.Sprintf("Working at %.2f MB/s (ETA: %.1fs)",speed,eta)
g.Update()
data = nil
}
if mode=="encrypt"{
@ -571,6 +763,111 @@ func work(){
fmt.Println("==============================")
resetUI()
status = "Completed."
working = false
}
// Generate file checksums
func generateChecksums(file string){
fin,_ := os.Open(file)
cs_md5 = ""
cs_sha1 = ""
cs_sha256 = ""
cs_sha3_256 = ""
cs_blake2b = ""
cs_blake2s = ""
cs_blake3 = ""
if md5_selected{
cs_md5 = "Calculating..."
}
if sha1_selected{
cs_sha1 = "Calculating..."
}
if sha256_selected{
cs_sha256 = "Calculating..."
}
if sha3_256_selected{
cs_sha3_256 = "Calculating..."
}
if blake2b_selected{
cs_blake2b = "Calculating..."
}
if blake2s_selected{
cs_blake2s = "Calculating..."
}
if blake3_selected{
cs_blake3 = "Calculating..."
}
crc_md5 := md5.New()
crc_sha1 := sha1.New()
crc_sha256 := sha256.New()
crc_sha3_256 := sha3.New256()
crc_blake2b,_ := blake2b.New256(nil)
crc_blake2s,_ := blake2s.New256(nil)
crc_blake3 := blake3.New()
stat,_ := os.Stat(file)
total := stat.Size()
var done int64 = 0
for{
var data []byte
_data := make([]byte,1048576)
size,err := fin.Read(_data)
if err!=nil{
break
}
data = _data[:size]
if md5_selected{
crc_md5.Write(data)
}
if sha1_selected{
crc_sha1.Write(data)
}
if sha256_selected{
crc_sha256.Write(data)
}
if sha3_256_selected{
crc_sha3_256.Write(data)
}
if blake2b_selected{
crc_blake2b.Write(data)
}
if blake2s_selected{
crc_blake2s.Write(data)
}
if blake3_selected{
crc_blake3.Write(data)
}
done += int64(size)
cs_progress = float32(done)/float32(total)
g.Update()
}
cs_progress = 0
if md5_selected{
cs_md5 = hex.EncodeToString(crc_md5.Sum(nil))
}
if sha1_selected{
cs_sha1 = hex.EncodeToString(crc_sha1.Sum(nil))
}
if sha256_selected{
cs_sha256 = hex.EncodeToString(crc_sha256.Sum(nil))
}
if sha3_256_selected{
cs_sha3_256 = hex.EncodeToString(crc_sha3_256.Sum(nil))
}
if blake2b_selected{
cs_blake2b = hex.EncodeToString(crc_blake2b.Sum(nil))
}
if blake2s_selected{
cs_blake2s = hex.EncodeToString(crc_blake2s.Sum(nil))
}
if blake3_selected{
cs_blake3 = hex.EncodeToString(crc_blake3.Sum(nil))
}
g.Update()
}
// Reset the UI to a clean state with no nothing selected
@ -585,6 +882,9 @@ func resetUI(){
keep = false
erase = false
reedsolo = false
split = false
splitSize = ""
fast = false
progress = 0
progressInfo = ""
g.Update()
@ -618,8 +918,7 @@ func rsDecode(data []byte,encoder reedsolomon.Encoder,size int) []byte{
// Create the master window, set callbacks, and start the UI
func main(){
window := g.NewMasterWindow("Picocrypt",480,470,g.MasterWindowFlagsNotResizable,nil)
window.SetBgColor(color.RGBA{0xf5,0xf6,0xf7,255})
window := g.NewMasterWindow("Picocrypt",480,466,g.MasterWindowFlagsNotResizable,nil)
window.SetDropCallback(onDrop)
dpi = g.Context.GetPlatform().GetContentScale()
window.Run(startUI)