Update Picocrypt.go

This commit is contained in:
Evan Su 2021-05-23 23:07:28 -04:00 committed by GitHub
parent 721c308c93
commit a847395fb1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 87 additions and 25 deletions

View File

@ -101,12 +101,14 @@ var password string
var cPassword string var cPassword string
var metadata string var metadata string
var keep bool var keep bool
var erase bool
var reedsolo bool var reedsolo bool
var split bool var split bool
var splitSize string var splitSize string
var fast bool var fast bool
var kept = false
// Reed-Solomon encoders // Reed-Solomon encoders
var rs5_128,_ = reedsolomon.New(5,128) var rs5_128,_ = reedsolomon.New(5,128)
var rs10_128,_ = reedsolomon.New(10,128) var rs10_128,_ = reedsolomon.New(10,128)
@ -148,6 +150,11 @@ func startUI(){
g.TabBar("TabBar").Layout( g.TabBar("TabBar").Layout(
// File encryption/decryption tab // File encryption/decryption tab
g.TabItem("Encryption/decryption").Layout( g.TabItem("Encryption/decryption").Layout(
g.PopupModal("Confirm").Layout(
g.Label("HI"),
g.Button("Yes").OnClick(func(){g.CloseCurrentPopup()}),
g.Button("No"),
),
// Update 'tab' to indicate active tab // Update 'tab' to indicate active tab
g.Custom(func(){ g.Custom(func(){
if g.IsItemActive(){ if g.IsItemActive(){
@ -214,12 +221,11 @@ func startUI(){
// Optional metadata // Optional metadata
g.Dummy(10,0), g.Dummy(10,0),
g.Label("Metadata (optional):"), g.Label("Metadata (optional):"),
g.InputTextMultiline("##metadata",&metadata).Size(226,100), g.InputTextMultiline("##metadata",&metadata).Size(226,126),
// Advanced options can be enabled with checkboxes // Advanced options can be enabled with checkboxes
g.Dummy(10,0), g.Dummy(10,0),
g.Checkbox("Keep decrypted output even if it's corrupted or modified",&keep), g.Checkbox("Keep decrypted output even if it's corrupted or modified",&keep),
g.Checkbox("Securely erase and delete original file(s)",&erase),
g.Row( g.Row(
g.Checkbox("Encode with Reed-Solomon to prevent corruption",&reedsolo), g.Checkbox("Encode with Reed-Solomon to prevent corruption",&reedsolo),
g.Button("?").OnClick(func(){ g.Button("?").OnClick(func(){
@ -442,6 +448,7 @@ func startUI(){
// Handle files dropped into Picocrypt by user // Handle files dropped into Picocrypt by user
func onDrop(names []string){ func onDrop(names []string){
_status = ""
if tab==0{ if tab==0{
// Clear variables // Clear variables
onlyFiles = nil onlyFiles = nil
@ -514,6 +521,10 @@ func onDrop(names []string){
} }
}else{ }else{
mode = "encrypt" mode = "encrypt"
// Show the ".pcv" file extension
orLabel = ".pcv or"
outputWidth = 341
// There are multiple dropped items, check each one // There are multiple dropped items, check each one
for _,name := range names{ for _,name := range names{
stat,_ := os.Stat(name) stat,_ := os.Stat(name)
@ -583,10 +594,16 @@ func work(){
var nonce []byte var nonce []byte
var keyHash []byte var keyHash []byte
var _keyHash []byte var _keyHash []byte
var crcHash []byte var keyfileHash []byte
var nonces []byte var nonces []byte
fmt.Println(mode)
// Check if output file already exists
stat,err := os.Stat(outputFile)
if err==nil{
confirmOverwrite()
}
// Set the output file based on mode // Set the output file based on mode
if mode=="encrypt"{ if mode=="encrypt"{
outputFile = outputEntry+".pcv" outputFile = outputEntry+".pcv"
@ -624,7 +641,7 @@ func work(){
} }
fmt.Println(inputFile) fmt.Println(inputFile)
stat,_ := os.Stat(inputFile) stat,_ = os.Stat(inputFile)
total := stat.Size() total := stat.Size()
fmt.Println(total) fmt.Println(total)
@ -689,7 +706,7 @@ func work(){
// Write placeholder for hash of key // Write placeholder for hash of key
fout.Write(make([]byte,192)) fout.Write(make([]byte,192))
// Write placeholder for Blake3 CRC // Write placeholder for hash of hash of keyfile
fout.Write(make([]byte,160)) fout.Write(make([]byte,160))
@ -736,9 +753,9 @@ func work(){
_keyHash = rsDecode(_keyHash,rs64_128,64) _keyHash = rsDecode(_keyHash,rs64_128,64)
//fmt.Println("keyHash",keyHash) //fmt.Println("keyHash",keyHash)
crcHash = make([]byte,160) keyfileHash = make([]byte,160)
fin.Read(crcHash) fin.Read(keyfileHash)
crcHash = rsDecode(crcHash,rs32_128,32) keyfileHash = rsDecode(keyfileHash,rs32_128,32)
//fmt.Println("crcHash",crcHash) //fmt.Println("crcHash",crcHash)
_tmp := math.Ceil(float64(total-int64(metadataLength+1196))/float64(1048728)) _tmp := math.Ceil(float64(total-int64(metadataLength+1196))/float64(1048728))
@ -782,11 +799,14 @@ func work(){
} }
} }
if !keyCorrect{ if !keyCorrect{
working = false if keep{
_status = "Incorrect password." kept = true
_status_color = color.RGBA{0xff,0x00,0x00,255} }else{
fout.Close()
broken()
return return
} }
}
fout,_ = os.OpenFile( fout,_ = os.OpenFile(
outputFile, outputFile,
os.O_RDWR|os.O_CREATE|os.O_TRUNC, os.O_RDWR|os.O_CREATE|os.O_TRUNC,
@ -795,7 +815,7 @@ func work(){
defer fout.Close() defer fout.Close()
} }
crc := blake3.New() //crc := blake3.New()
done := 0 done := 0
counter := 0 counter := 0
@ -825,8 +845,21 @@ func work(){
}*/ }*/
//fmt.Println("ENCRYPTED NONCES: ",tmp) //fmt.Println("ENCRYPTED NONCES: ",tmp)
// XXXXXXXXXXXXXXXXFSFSDFFFSFF // XXXXXXXXXXXXXXXXFSFSDFFFSFF
//nonces,_ = cipher.Open(nil,nonce,tmp,nil) //var err error
nonces,_ = monocypher.Unlock(nonces,nonce,key,_mac) //nonces,err = cipher.Open(nil,nonce,tmp,nil)
//fmt.Println(err)
var authentic bool
nonces,authentic = monocypher.Unlock(tmp,nonce,key,_mac)
if !authentic{
if keep{
kept = true
}else{
working = false
_status = "The file is either corrupted or intentionally modified."
_status_color = color.RGBA{0xff,0x00,0x00,255}
return
}
}
//fmt.Println("UNENCRYPTED NONCES: ",nonces) //fmt.Println("UNENCRYPTED NONCES: ",nonces)
} }
for{ for{
@ -873,8 +906,8 @@ func work(){
mac,data := monocypher.Lock(data,_nonce,key) mac,data := monocypher.Lock(data,_nonce,key)
fout.Write(data) fout.Write(data)
fout.Write(mac) fout.Write(mac)
crc.Write(data) //crc.Write(data)
crc.Write(mac) //crc.Write(mac)
} }
//fout.Write(data) //fout.Write(data)
@ -884,10 +917,20 @@ func work(){
if fast{ if fast{
data,_ = cipher.Open(nil,_nonce,data,nil) data,_ = cipher.Open(nil,_nonce,data,nil)
}else{ }else{
crc.Write(data) //crc.Write(data)
mac := data[len(data)-16:] mac := data[len(data)-16:]
data = data[:len(data)-16] data = data[:len(data)-16]
data,_ = monocypher.Unlock(data,_nonce,key,mac) var authentic bool
data,authentic = monocypher.Unlock(data,_nonce,key,mac)
if !authentic{
if keep{
kept = true
}else{
fout.Close()
broken()
return
}
}
} }
fout.Write(data) fout.Write(data)
//fmt.Println(authentic) //fmt.Println(authentic)
@ -915,9 +958,10 @@ func work(){
//fmt.Println("'nonces' before RS: ",nonces) //fmt.Println("'nonces' before RS: ",nonces)
fout.Seek(int64(700+len(metadata)),0) fout.Seek(int64(700+len(metadata)),0)
fout.Write(rsEncode(keyHash,rs64_128,192)) fout.Write(rsEncode(keyHash,rs64_128,192))
fout.Write(rsEncode(crc.Sum(nil),rs32_128,160)) fout.Write(rsEncode(make([]byte,32),rs32_128,160))
_mac,tmp := monocypher.Lock(nonces,nonce,key) _mac,tmp := monocypher.Lock(nonces,nonce,key)
fmt.Println(_mac)
//tmp := cipher.Seal(nil,nonce,nonces,nil) //tmp := cipher.Seal(nil,nonce,nonces,nil)
//fmt.Println("ENCRYPTED NONCES: ",tmp) //fmt.Println("ENCRYPTED NONCES: ",tmp)
//_mac := tmp[len(tmp)-16:] //_mac := tmp[len(tmp)-16:]
@ -946,9 +990,15 @@ func work(){
} }
fmt.Println("==============================") fmt.Println("==============================")
resetUI() resetUI()
if kept{
_status = "The input is corrupted and/or modified. Please be careful."
_status_color = color.RGBA{0xff,0xff,0x00,255}
}else{
_status = "Completed." _status = "Completed."
_status_color = color.RGBA{0x00,0xff,0x00,255} _status_color = color.RGBA{0x00,0xff,0x00,255}
}
working = false working = false
kept = false
} }
// Generate file checksums // Generate file checksums
@ -1065,16 +1115,28 @@ func resetUI(){
cPassword = "" cPassword = ""
metadata = "" metadata = ""
keep = false keep = false
erase = false
reedsolo = false reedsolo = false
split = false split = false
splitSize = "" splitSize = ""
fast = false fast = false
progress = 0 progress = 0
progressInfo = "" progressInfo = ""
_status = ""
_status_color = color.RGBA{0xff,0xff,0xff,255}
g.Update() g.Update()
} }
func confirmOverwrite(){
g.OpenPopup("Confirm")
}
func broken(){
working = false
_status = "The file is either corrupted or intentionally modified."
_status_color = color.RGBA{0xff,0x00,0x00,255}
os.Remove(outputFile)
}
func rsEncode(data []byte,encoder reedsolomon.Encoder,size int) []byte{ func rsEncode(data []byte,encoder reedsolomon.Encoder,size int) []byte{
shards,_ := encoder.Split(data) shards,_ := encoder.Split(data)
encoder.Encode(shards) encoder.Encode(shards)