I have the following code where I have a slice of bytes with the alphabet, I copy this alphabet array in a new variable (cryptkey) and I use a function to shuffle it. The result is that the alphabet and the cryptkey byte slice get shuffled. How can I prevent this from happening?
package main
import (
"fmt"
"math/rand"
)
func main() {
alphabet := []byte("ABCDEFGHIJKLMNOPQRSTUVWXYZ abcdefghijklmnopqrstuvwxyz.")
cryptkey := alphabet
fmt.Println(string(alphabet))
cryptkey = shuffle(cryptkey)
fmt.Println(string(alphabet))
}
func shuffle(b []byte) []byte {
l := len(b)
out := b
for key := range out {
dest := rand.Intn(l)
out[key], out[dest] = out[dest], out[key]
}
return out
}
Result :
ABCDEFGHIJKLMNOPQRSTUVWXYZ abcdefghijklmnopqrstuvwxyz. miclOfEInzJNvZe.YuVMCdTbXyqtaLwHGjUrABhog xQPWSpKRkDsF