~stick/challenges

5135be9bb1f28d295d29e5f8258da32a70e62489 — Stick 2 months ago 6720466
convert.go
1 files changed, 73 insertions(+), 0 deletions(-)

A all_army_cyber_stakes/2020/convert.go
A all_army_cyber_stakes/2020/convert.go => all_army_cyber_stakes/2020/convert.go +73 -0
@@ 0,0 1,73 @@
package main

import (
	"encoding/base64"
	"encoding/hex"
	"fmt"
	"log"
	"math/big"
	"os"
)

const (
	RAW string = "raw"
	B64        = "b64"
	HEX        = "hex"
	DEC        = "dec"
	OCT        = "oct"
	BIN        = "bin"
)

func genResponse(line string, from string, to string) string {
	var data []byte
	var err error
	if from == B64 {
		data, err = base64.StdEncoding.DecodeString(line)
	} else if from == HEX {
		data, err = hex.DecodeString(line)
	} else if from == DEC {
		num := new(big.Int)
		num.SetString(line, 10)
		data = num.Bytes()
	} else if from == OCT {
		num := new(big.Int)
		num.SetString(line, 8)
		data = num.Bytes()
	} else if from == BIN {
		num := new(big.Int)
		num.SetString(line, 2)
		data = num.Bytes()
	} else {
		data = []byte(line)
	}
	if err != nil {
		log.Fatal(err)
	}

	var response string
	if to == B64 {
		response = base64.StdEncoding.EncodeToString(data)
	} else if to == HEX {
		response = hex.EncodeToString(data)
	} else if to == DEC {
		num := new(big.Int)
		num.SetBytes(data)
		response = num.Text(10)
	} else if to == OCT {
		num := new(big.Int)
		num.SetBytes(data)
		response = num.Text(8)
	} else if to == BIN {
		num := new(big.Int)
		num.SetBytes(data)
		response = num.Text(2)
	} else {
		response = string(data)
	}

	return response
}

func main() {
	fmt.Println(genResponse(os.Args[3], os.Args[1], os.Args[2]))
}