package main
import (
"crypto/aes"
//"crypto/cipher"
//"crypto/rand"
"crypto/sha256"
"fmt"
"log"
//"io"
"encoding/hex"
//"io/ioutil"
"os"
)
func encrypt() {
//block size for SHA-256 is 64 bytes
blockSize := 64
keyMacHex := "6b6579" //TODO: parse from command line (last 16 bytes)
//parse hexadecimal key string into byte slice
keyMac, err := hex.DecodeString(keyMacHex)
if err != nil {
log.Fatal(err)
}
//generate inner and outer padded key
var outPad []byte
var inPad []byte
for i := 0; i < blockSize; i++ {
//XOR bits of key and pads (key is assumed padded with 0s if too short)
if i < len(keyMac) {
outPad = append(outPad, keyMac[i] ^ '\x5c')
inPad = append(inPad, keyMac[i] ^ '\x36')
} else {
outPad = append(outPad, '\x00' ^ '\x5c')
inPad = append(inPad, '\x00' ^ '\x36')
}
}
msgHex := fmt.Sprintf("%x", "The quick brown fox jumps over the lazy dog") //TODO: parse from file
//parse hexadecimal message string into byte slice
msg, err := hex.DecodeString(msgHex)
if err != nil {
log.Fatal(err)
}
//two hash objects for the inner and outer hash
h256_inner := sha256.New()
h256_outer := sha256.New()
//hash the concatenation of inner pad and message
inMsg := append(inPad, msg...)
h256_inner.Write(inMsg)
//hash the concatenation of outer pad and inner hash
outMsg := append(outPad, h256_inner.Sum(nil)...)
h256_outer.Write(outMsg)
//get final 32-byte MAC tag
mac := h256_outer.Sum(nil)
//concatenate message and MAC tag
mPad1 := append(msg, mac...)
//fmt.Printf("%x\n", msg)
//get padding string using PKCS #5 to make message a multiple of AES block size (16)
n := len(mPad1) % 16
var pStr []byte
for i := 0; i < (16 - n); i++ {
pStr = append(pStr, byte(16-n))
}
mPad2 := append(mPad1, pStr...)
blockIndex := 0
var cipherText []byte
//test key
key := []byte{'\x03', '\x03', '\x03', '\x03', '\x03', '\x03', '\x03', '\x03', '\x03', '\x03', '\x03', '\x03', '\x03', '\x03', '\x03', '\x03'}
//test IV
iv := [16]byte{'\x03', '\x03', '\x03', '\x03', '\x03', '\x03', '\x03', '\x03', '\x03', '\x03', '\x03', '\x03', '\x03', '\x03', '\x03', '\x03'}
//encrypt for every block in plaintext
for blockIndex < len(mPad2) {
//initialize new slice to hold current block's XOR
var xorText []byte
for i := blockIndex; i < blockIndex + 16; i++ {
//use IV bits to XOR in first block
//use previous ciphertext block to XOR for remaining blocks
if blockIndex == 0 {
xorText = append(xorText, iv[i] ^ mPad2[i])
} else {
xorText = append(xorText, cipherText[i-16] ^ mPad2[i])
}
}
block, err := aes.NewCipher(key) //key should be 16 bytes of input parameter
if err != nil {
log.Fatal(err)
}
//encrypt XOR'ed block
cipherBlock := make([]byte, 16)
block.Encrypt(cipherBlock, xorText)
//append to current ciphertext
cipherText = append(cipherText, cipherBlock...)
//increment starting index of block
blockIndex += 16
}
fmt.Printf("%x\n", cipherText)
}
func decrypt() {
}
func main() {
if len(os.Args) < 2 {
log.Fatal("Invalid # of arguments.")
}
mode := os.Args[1]
if mode == "encrypt" {
encrypt()
} else if mode == "decrypt" {
decrypt()
} else {
log.Fatal("Invalid mode.")
}
}
About Online Golang Compiler
Try our Online Golang Compiler (Version Go v1.18.1) to Edit, Run, and Share your Golang Code directly from your browser. This online development environment provides you the latest version Go v1.18.1.
How to use Online Golang Compiler?
Write and Execute Code
- Write your program (or, paste it) directly under the "Source Code" tab.
- If you want to save your program, go to the "Project" menu and save it.
- You can directly execute your program without saving it by clicking on on "Execute" button.
User Input
The latest version of Coding Ground allows to provide program input at run time from the termnial window exactly the same way as you run your program at your own computer. So simply run a program and provide your program input (if any) from the terminal window available in the right side.
Online Golang Compiler: Keyboard Shortcuts
The following are the keyword shortcut of this Online Golang Compiler:
Shortcut | Description |
⌘ + Enter | Run the program |
⌘ + S | Save Project (Login Required) |
⇧ + ⌘ + S | Save As Project |
⌘ + P | New Project |
⌘ + G | Share Project |
⌘ + Z | Undo Editing |
⌘ + Y | Redo Editing |
⌘ + A | Select All Text |
⌘ + X | Cut Selected Text |
⌘ + C | Copy Selected Text |
⌘ + V | Paste Copied Text |
⌘ + F | Search Text |
⌘ + ⌥ + F | Replace Text |
Shortcut | Description |
Ctrl + Enter | Run the program |
Ctrl + S | Save Project |
Shift + Ctrl + S | Save As Project |
Ctrl + G | Share Project |
Ctrl + Z | Undo Editing |
Ctrl + Y | Redo Editing |
Ctrl + A | Select All Text |
Ctrl + X | Cut Selected Text |
Ctrl + C | Copy Selected Text |
Ctrl + V | Paste Copied Text |
Ctrl + F | Search Text |
Ctrl + H | Replace Text |
Online Golang Compiler: Save and Share Golang Code (Project)
Save Golang Project Online
You can save your Golang Project with us so that you can access this project later on. To save a project you will need to create a login Id with us. So before you save a project, please create a login Id using a link given at the top right corner of this page.
Share Golang Project Online
You can use this feature to share your Golang Code with your teachers, classmates and colleagues. Just click Share Button and it will create a short link, which can be shared through Email, WhatsApp or even through Social Media. A shared link will be deleted if it has been passive for almost 3 months.
More Features of Online Golang Compiler
- Theme – You can change the current editor's theme from the "Editor Theme" option under "Settings" menu.
- Font Size – You can change the font size of the editor /compiler from from the "Font Size" option under "Settings" menu.
- Tab Size – You can change the tab size from the "Tab Size" option under "Settings" Menu.
- Show/Hide Line Numbers – You can show/hide the line number with the code from the "Show Line Numbers" or "Hide Line Numbers" option under "Settings" Menu.
- And, many more.
Benefits of Using Online Golang Compiler
There are several benefits of using the Online Golang Compiler to run your Golang code:
- Platform independence: You can run your code from any device without taking care of operating systems.
- Convenience: You don't need to install anything for using this.
- No setup required: There is no need for additional setup to run your code.
- Updated version: Our online compiler/editors/terminals are the latest up-to-date.