I want to encrypt a string with Go, my actual code is:
package main
import (
"fmt"
)
const key = "\xbd\xb2\x3d\xbc\x20\xe2\x8c\x98" //some random numbers here
func Encrypt(input string) (output string) {
for i := 0; i < len(input); i++ {
output += fmt.Sprintf("\\x%02x", input[i] ^ key[i % len(key)])
}
return output;
}
func Decrypt(input string) (output string) {
key := "\xbd\xb2\x3d\xbc\x20\xe2\x8c\x98"
for i := 0; i < len(input); i++ {
output += string(input[i] ^ key[i % len(key)])
}
return output;
}
func main() {
stringa := "password"
encrypted := Encrypt(stringa)
fmt.Println(encrypted)
fmt.Println(Decrypt(encrypted))
fmt.Println(stringa)
}
\xcd\xd3\x4e\xcf\x57\x8d\xfe\xfc
áE^O|?è«áE U|?ï_á?|?'üáE[U|?êû
password
Problem is after encrypt string, when I try to decrypt return different output. Where did I go wrong?
It looks like your goal is to xor the the bytes in a string with the bytes in a key. Here's one way to do it:
func xor(input string) string {
output := make([]byte, len(input))
for i := 0; i < len(input); i++ {
output[i] = input[i] ^ key[i%len(key)]
}
return string(output)
}
The Encrypt and Decrypt functions are the same:
func Encrypt(input string) string { return xor(input) }
func Decrypt(input string) string { return xor(input) }
Related
package main
import (
"fmt"
"reflect"
)
type Aservice struct {
}
type Adata struct {
msg string
}
type Bdata struct {
more string
}
var amap map[string]interface{} = make(map[string]interface{}, 1024)
func (aser *Aservice) Bar(data *Adata) error {
return nil
}
func (aser *Aservice) Foo(data *Bdata) error {
return nil
}
func main() {
var ser *Aservice
typeOfService := reflect.TypeOf(ser)
valueOfService := reflect.ValueOf(ser)
for i := 0; i < valueOfService.NumMethod(); i++ {
nref := valueOfService.Method(i).Type().In(0)
fmt.Println("++", nref.Elem().Name())
amap[typeOfService.Method(i).Name] = nref
}
}
Currently "Adata" and "Bdata" can be printed correctly
But I don’t know how to store the empty structure pointers of "Adata" and "Bdata" in amap
No idea for the next step
I want to use Method(i).Name() in amap to store the parameters that need to be passed in for the Method
Based on the suggestions in comments :
package main
import (
"fmt"
"reflect"
)
type Aservice struct {
}
type Adata struct {
msg string
}
type Bdata struct {
more string
}
var amap = map[string]interface{}{}
func (aser *Aservice) Bar(data *Adata) error {
return nil
}
func (aser *Aservice) Foo(data *Bdata) error {
return nil
}
func main() {
var ser *Aservice
typeOfService := reflect.TypeOf(ser)
valueOfService := reflect.ValueOf(ser)
for i := 0; i < valueOfService.NumMethod(); i++ {
nref := valueOfService.Method(i).Type().In(0)
amap[typeOfService.Method(i).Name] = reflect.New(nref.Elem()).Interface()
}
for k, v := range amap {
fmt.Printf("%s %#v\n", k, v)
}
}
Output:
Bar &main.Adata{msg:""}
Foo &main.Bdata{more:""}
package main
import "fmt"
func main() {
var kata, kosong, kebalikan string
fmt.Print("Kata :")
fmt.Scan(&kata)
panjang := len(kata)
for i := panjang - 1; i >= 0; i-- {
kebalikan := kosong + fmt.Print(string(kata[i]))
}
if kata == kebalikan {
fmt.Println("\n", true)
} else {
fmt.Println("\n", false)
}
}
does anybody know how to store kosong + fmt.Print(string(kata[i])) to kebalikan ? just new in golang
the error is multiple-value fmt.Print() in single-value context
There are two problems with your code.
You need to use fmt.Sprint instead of fmt.Print in your case. The former returns a string, the latter prints it to stdout.
Do not use : in kebalikan := kosong + fmt.Print(string(kata[i])). You do not want to create a new local variable, instead modify the existing variable.
Here is the fixed code:
package main
import "fmt"
func main() {
var kata, kosong, kebalikan string
fmt.Print("Kata :")
fmt.Scan(&kata)
panjang := len(kata)
for i := panjang - 1; i >= 0; i-- {
kebalikan = kosong + fmt.Sprint(string(kata[i]))
}
if kata == kebalikan {
fmt.Println("\n", true)
} else {
fmt.Println("\n", false)
}
}
Does Go have any method or there is a suggestion how to check if a string contains only ASCII characters? What is the right way to do it?
From my research, one of the solution is to check whatever there is any char greater than 127.
func isASCII(s string) bool {
for _, c := range s {
if c > unicode.MaxASCII {
return false
}
}
return true
}
In Go, we care about performance, Therefore, we would benchmark your code:
func isASCII(s string) bool {
for _, c := range s {
if c > unicode.MaxASCII {
return false
}
}
return true
}
BenchmarkRange-4 20000000 82.0 ns/op
A faster (better, more idiomatic) version, which avoids unnecessary rune conversions:
func isASCII(s string) bool {
for i := 0; i < len(s); i++ {
if s[i] > unicode.MaxASCII {
return false
}
}
return true
}
BenchmarkIndex-4 30000000 55.4 ns/op
ascii_test.go:
package main
import (
"testing"
"unicode"
)
func isASCIIRange(s string) bool {
for _, c := range s {
if c > unicode.MaxASCII {
return false
}
}
return true
}
func BenchmarkRange(b *testing.B) {
str := ascii()
b.ResetTimer()
for N := 0; N < b.N; N++ {
is := isASCIIRange(str)
if !is {
b.Fatal("notASCII")
}
}
}
func isASCIIIndex(s string) bool {
for i := 0; i < len(s); i++ {
if s[i] > unicode.MaxASCII {
return false
}
}
return true
}
func BenchmarkIndex(b *testing.B) {
str := ascii()
b.ResetTimer()
for N := 0; N < b.N; N++ {
is := isASCIIIndex(str)
if !is {
b.Log("notASCII")
}
}
}
func ascii() string {
byt := make([]byte, unicode.MaxASCII+1)
for i := range byt {
byt[i] = byte(i)
}
return string(byt)
}
Output:
$ go test ascii_test.go -bench=.
BenchmarkRange-4 20000000 82.0 ns/op
BenchmarkIndex-4 30000000 55.4 ns/op
$
It looks like your way is best.
ASCII is simply defined as:
ASCII encodes 128 specified characters into seven-bit integers
As such, characters have values 0-27 (or 0-127, 0x0-0x7F).
Go provides no way to check that every rune in a string (or byte in a slice) has numerical values in a specific range, so your code seems to be the best way to do it.
Another option:
package main
import "golang.org/x/exp/utf8string"
func main() {
{
b := utf8string.NewString("south north").IsASCII()
println(b) // true
}
{
b := utf8string.NewString("🧡💛💚💙💜").IsASCII()
println(b) // false
}
}
https://pkg.go.dev/golang.org/x/exp/utf8string#String.IsASCII
I have two different struct as mentioned below A abd B and two process functions. Is there any way by means of which i can write a common function to generate the map[string]struct for the both the struct. Moreover, is there any way using reflection given the struct name i can create the object of the same?
type A struct {
name string
// more fields
}
type B struct {
name string
// more fields
}
func ProcessA(input []A) map[string]A {
output := make(map[string]A)
for _, v := range input {
output[v.name] = v
}
return output
}
func ProcessB(input []B) map[string]B {
output := make(map[string]B)
for _, v := range input {
output[v.name] = v
}
return output
}
Idiomatic way in Go would be to use interface.
type Named interface {
Name() string
}
type letter struct {
name string
}
func (l letter) Name() string {
return l.name
}
type A struct {
letter
// more fields
}
type B struct {
letter
// more fields
}
func ProcessNameds(input []Named) map[string]Named {
output := make(map[string]Named, len(input))
for _, v := range input {
output[v.Name()] = v
}
return output
}
Well, see if something like this would help:
package main
import (
"fmt"
"strconv"
)
type A struct {
name string
// more fields
}
type B struct {
name string
// more fields
}
func Process(x interface{}) interface{} {
ma := make(map[string]int)
mb := make(map[string]string)
if x == nil {
return nil
} else if a, ok := x.([]A); ok {
fmt.Printf("Type A argument passed %s\n", x)
ma[a[0].name] = 1
ma[a[1].name] = 2
return ma //you can return whatever type you want here
} else if b, ok := x.([]B); ok {
fmt.Printf("Type B argument passed %s\n", x)
mb[b[0].name] = "a"
mb[b[1].name] = "b"
return mb //you can return whatever type you want here
} else {
panic(fmt.Sprintf("Unexpected type %T: %v", x, x))
}
return nil
}
func main() {
a := make([]A, 5)
for i := 0; i < len(a); i++ {
a[i].name = strconv.Itoa(i) + "A"
}
b := make([]B, 7)
for i := 0; i < len(b); i++ {
b[i].name = strconv.Itoa(i) + "B"
}
fmt.Println(Process(a))
fmt.Println(Process(b))
//Uncomment line below to see the panic
//fmt.Println(Process(8))
}
https://play.golang.org/p/irdCsbpvUv_t
I have two structs:
type A struct {
BankCode string `json:"bankCode"`
BankName string `json:"bankName"`
}
And:
type B struct {
A
extra string `json:" extra"`
}
And two slices:
listsA []A and listsB []B
I want to get bankCodes from listA and listB. bankcodes only contains bankcodes. It is a []string
It will be so easy as using two function.
func getBankCodes(data []A) []string {
res := make([]string, len(data))
for i := 0; i < len(data); i++ {
res[i] = data[i].BankCode
}
return res
}
func getBankCodes(data []B) []string {
res := make([]string, len(data))
for i := 0; i < len(data); i++ {
res[i] = data[i].BankCode
}
return res
}
How to use one common function ?
Well the clean solution would be to use an interface, since go doesn't support classic inheritance, so something like []parentclass can't work. Interfaces however can only describe functions not a common field, so you have to implement a Getter (essentially).
// GetBankCoder provides a function that gives the BankCode
type GetBankCoder interface {
getBankCode() string
}
// implement GetBankCoder for A (and indirectly for B)
func (a A) getBankCode() string {
return a.BankCode
}
and make your getBankCodes work on that interface type, notice the parameter of the function as well as the statement inside the loop:
func getBankCodes(data []GetBankCoder) []string { // <-- changed
res := make([]string, len(data))
for i := 0; i < len(data); i++ {
res[i] = data[i].getBankCode() // <-- changed
}
return res
}
There are other solutions where the function parameter is of interface{} type and then reflection is used to assure you can actually do .BankCode, but I don't like those, as they are not adding more clarity either.
... However, I couldn't get the golang playground to make this work correctly without putting it into a []GetBankCoder var first, before giving it to the function.
banks := make([]GetBankCoder, 0)
banks = append(banks, A{ BankCode: "ABC", BankName: "ABC Bank"})
getBankCodes(banks)
You may use one common function like so:
func BankCodes(data interface{}) []string {
if reflect.TypeOf(data).Kind() != reflect.Slice {
panic("err: data is not slice")
}
slice := reflect.Indirect(reflect.ValueOf(data))
res := make([]string, slice.Len())
for i := 0; i < slice.Len(); i++ {
a := slice.Index(i).Interface().(BankCoder)
res[i] = a.Bankcode()
}
return res
}
Code (try on The Go Playground):
package main
import (
"fmt"
"reflect"
)
func main() {
bs := []B{B{A{"BC1", "BN"}, "e"}, B{A{"BC2", "BN"}, "e"}}
strs := BankCodes(bs)
fmt.Println(strs)
as := []A{A{"AC1", "BN"}, A{"AC2", "BN"}}
strs2 := BankCodes(as)
fmt.Println(strs2)
}
func BankCodes(data interface{}) []string {
if reflect.TypeOf(data).Kind() != reflect.Slice {
panic("err: data is not slice")
}
slice := reflect.Indirect(reflect.ValueOf(data))
res := make([]string, slice.Len())
for i := 0; i < slice.Len(); i++ {
a := slice.Index(i).Interface().(BankCoder)
res[i] = a.Bankcode()
}
return res
}
type A struct {
BankCode string `json:"bankCode"`
BankName string `json:"bankName"`
}
type B struct {
A
extra string `json:" extra"`
}
type BankCoder interface {
Bankcode() string
}
func (a A) Bankcode() string {
return a.BankCode
}