diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2684e84 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +Go/dotp diff --git a/Go/go.mod b/Go/go.mod index 37e6991..b054230 100644 --- a/Go/go.mod +++ b/Go/go.mod @@ -1,3 +1,3 @@ -module one-time-pad-utils +module dotp go 1.19 diff --git a/Go/main.go b/Go/main.go index dc9e89f..0823cc7 100644 --- a/Go/main.go +++ b/Go/main.go @@ -1,16 +1,16 @@ package main import ( - "fmt" + otpdecrypt "dotp/otp_decrypt" + otpencrypt "dotp/otp_encrypt" + otpgenerate "dotp/otp_generate" "flag" + "fmt" "math" "strings" - "one-time-pad-utils/otp_generate" - "one-time-pad-utils/otp_encrypt" - "one-time-pad-utils/otp_decrypt" ) -func main(){ +func main() { var genpad = flag.Bool("generate", false, "Generate a new One-Time Pad using CSPRNG") var genpadchunks = flag.Int("chunks", 200, "Specify the amount of chunks to generate") var encryptmessage = flag.String("encrypt", "", "Specify a message you wish to encrypt (use quotes if you have spaces)") @@ -25,11 +25,11 @@ func main(){ } if *encryptmessage != "" { - if *otpkey != ""{ - fmt.Printf("%v\n", otpencrypt.OTPEncrypt(*encryptmessage,*otpkey)) + if *otpkey != "" { + fmt.Printf("%v\n", otpencrypt.OTPEncrypt(*encryptmessage, *otpkey)) } else { encryptmessagestring := strings.Replace(*encryptmessage, " ", "", -1) - lenofmessage := math.Ceil(float64(len(encryptmessagestring))/float64(5)) + lenofmessage := math.Ceil(float64(len(encryptmessagestring)) / float64(5)) generated_key := otpgenerate.GenerateOTP(int(lenofmessage)) fmt.Printf("GENERATED NEW KEY SINCE NONE WAS SPECIFIED:\n%v\n\n%v\n", generated_key, otpencrypt.OTPEncrypt(*encryptmessage, generated_key)) } @@ -37,13 +37,13 @@ func main(){ } if *decryptmessage != "" { - if *otpkey != ""{ - fmt.Printf("%v\n", otpdecrypt.OTPDecrypt(*decryptmessage,*otpkey)) + if *otpkey != "" { + fmt.Printf("%v\n", otpdecrypt.OTPDecrypt(*decryptmessage, *otpkey)) } else { fmt.Println("PLEASE SUPPLY A KEY") } return } - + flag.PrintDefaults() } diff --git a/Go/one-time-pad-utils b/Go/one-time-pad-utils deleted file mode 100755 index 6ea9ba6..0000000 Binary files a/Go/one-time-pad-utils and /dev/null differ diff --git a/Go/otp_decrypt/otp_decrypt.go b/Go/otp_decrypt/otp_decrypt.go index 8f6f082..f806d7e 100644 --- a/Go/otp_decrypt/otp_decrypt.go +++ b/Go/otp_decrypt/otp_decrypt.go @@ -1,21 +1,21 @@ package otpdecrypt import ( + paddefinitions "dotp/pad_definitions" "strings" - "one-time-pad-utils/pad_definitions" ) func OTPDecrypt(message string, key string) string { message = strings.Replace(message, " ", "", -1) - messagesplit := strings.Split(message,"") + messagesplit := strings.Split(message, "") var messagesplitint []int key = strings.Replace(key, " ", "", -1) - keysplit := strings.Split(key,"") + keysplit := strings.Split(key, "") var keysplitint []int for _, char := range messagesplit { - for n, c := range paddefinitions.NumToCharMap{ + for n, c := range paddefinitions.NumToCharMap { if c == strings.ToUpper(char) { messagesplitint = append(messagesplitint, n) @@ -24,7 +24,7 @@ func OTPDecrypt(message string, key string) string { } for _, char := range keysplit { - for n, c := range paddefinitions.NumToCharMap{ + for n, c := range paddefinitions.NumToCharMap { if c == strings.ToUpper(char) { keysplitint = append(keysplitint, n) @@ -38,8 +38,8 @@ func OTPDecrypt(message string, key string) string { var decryptedmessage []string - for pos, messagenum := range messagesplitint{ - decryptedmessage = append(decryptedmessage, paddefinitions.NumToCharMap[(((messagenum - keysplitint[pos]) % 36) + 36) % 36]) // crazy modulo for decryption + for pos, messagenum := range messagesplitint { + decryptedmessage = append(decryptedmessage, paddefinitions.NumToCharMap[(((messagenum-keysplitint[pos])%36)+36)%36]) // crazy modulo for decryption } diff --git a/Go/otp_encrypt/otp_encrypt.go b/Go/otp_encrypt/otp_encrypt.go index 246e055..c6879b7 100644 --- a/Go/otp_encrypt/otp_encrypt.go +++ b/Go/otp_encrypt/otp_encrypt.go @@ -1,21 +1,21 @@ package otpencrypt import ( + paddefinitions "dotp/pad_definitions" "strings" - "one-time-pad-utils/pad_definitions" ) func OTPEncrypt(message string, key string) string { message = strings.Replace(message, " ", "", -1) - messagesplit := strings.Split(message,"") + messagesplit := strings.Split(message, "") var messagesplitint []int key = strings.Replace(key, " ", "", -1) - keysplit := strings.Split(key,"") + keysplit := strings.Split(key, "") var keysplitint []int for _, char := range messagesplit { - for n, c := range paddefinitions.NumToCharMap{ + for n, c := range paddefinitions.NumToCharMap { if c == strings.ToUpper(char) { messagesplitint = append(messagesplitint, n) @@ -24,7 +24,7 @@ func OTPEncrypt(message string, key string) string { } for _, char := range keysplit { - for n, c := range paddefinitions.NumToCharMap{ + for n, c := range paddefinitions.NumToCharMap { if c == strings.ToUpper(char) { keysplitint = append(keysplitint, n) @@ -38,11 +38,11 @@ func OTPEncrypt(message string, key string) string { var encryptedmessage []string - for pos, messagenum := range messagesplitint{ - if pos != 0 && pos % 5 == 0 { + for pos, messagenum := range messagesplitint { + if pos != 0 && pos%5 == 0 { encryptedmessage = append(encryptedmessage, " ") } - encryptedmessage = append(encryptedmessage, paddefinitions.NumToCharMap[(messagenum + keysplitint[pos]) % 36]) + encryptedmessage = append(encryptedmessage, paddefinitions.NumToCharMap[(messagenum+keysplitint[pos])%36]) } return (strings.Join(encryptedmessage, "")) diff --git a/Go/otp_generate/otp_gen.go b/Go/otp_generate/otp_gen.go index fb8e4d1..b25c65e 100644 --- a/Go/otp_generate/otp_gen.go +++ b/Go/otp_generate/otp_gen.go @@ -4,29 +4,28 @@ package otpgenerate import ( "crypto/rand" + paddefinitions "dotp/pad_definitions" "math/big" - "one-time-pad-utils/pad_definitions" ) - func GenerateOTP(chunks int) string { count := 1 otpstring := "" - for count < (chunks*5)+1{ + for count < (chunks*5)+1 { n, err := rand.Int(rand.Reader, big.NewInt(36)) // generate new cryptographically secure random number if err != nil { panic(err) } otpstring += paddefinitions.NumToCharMap[int(n.Int64())] // print that number using the character map - if count % 5 == 0 { // add a space every 5 characters, newline after 10 chunks - if count % 50 == 0{ + if count%5 == 0 { // add a space every 5 characters, newline after 10 chunks + if count%50 == 0 { otpstring += "\n" } else { otpstring += " " } } - count+=1 + count += 1 } return otpstring diff --git a/Go/pad_definitions/pad_definitions.go b/Go/pad_definitions/pad_definitions.go index 4e79594..9e70a00 100644 --- a/Go/pad_definitions/pad_definitions.go +++ b/Go/pad_definitions/pad_definitions.go @@ -1,16 +1,16 @@ package paddefinitions var NumToCharMap = map[int]string{ - 0: "A", - 1: "B", - 2: "C", - 3: "D", - 4: "E", - 5: "F", - 6: "G", - 7: "H", - 8: "I", - 9: "J", + 0: "A", + 1: "B", + 2: "C", + 3: "D", + 4: "E", + 5: "F", + 6: "G", + 7: "H", + 8: "I", + 9: "J", 10: "K", 11: "L", 12: "M",