0

Salsa20 requires a key *[32]byte which I want to provide using a string constant. Passing the key using a type-cast to a slice yield error: argument 4 has incompatible type. I don't know of any way to cast a string (or a slice) as an array.

I don't want to create variable byteKey [32]byte and use copy([:]byteKey, []byte(stringKey)) every time the encryption takes place.

This is my current solution:

func createFeedbackKey() (key [32]byte) {
  const s = "thesingleonegreatairfysecretword"
  copy(key[:], s)
  return
}

var feedbackKey = createFeedbackKey()

That's 6 lines of code with a copy where, imho, a mere typecast should suffice.

Is there a shorter/better way? Something like this maybe?

var key = [...]byte("0123456789ABCDEF0123456789ABCDEF")
4
  • @tim cooper Can you explain why this is a duplicate? My problem isn't solved by using a variable. I want to initialize an array of bytes using a string. Commented Mar 8, 2019 at 9:34
  • [...] doesn't help unless the bytes are specified individually. Commented Mar 8, 2019 at 9:42
  • There is no such thing as a constant array in Go, so your question cannot be answered. Read the linked question and try posting another question that isn't about constant arrays. Commented Mar 8, 2019 at 13:51
  • Ok. Thank you for explaining. I included the const aspect in my original question to better describe what i want to achive conceptually. Commented Mar 9, 2019 at 0:08

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.