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")
yogo1212
  • 93
  • 5
  • @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. – yogo1212 Mar 08 '19 at 09:34
  • `[...]` doesn't help unless the bytes are specified individually. – yogo1212 Mar 08 '19 at 09: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. –  Mar 08 '19 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. – yogo1212 Mar 09 '19 at 00:08

0 Answers0