# Create HS256 in linux command

**URL:** <https://forum.elixirforum.com/t/create-hs256-in-linux-command/41854>\
**Category:** Questions\
**Tags:** encryption\
**Created:** [August 23, 2021, 10:36am UTC](https://forum.elixirforum.com/t/create-hs256-in-linux-command/41854 "2021-08-23T10:36:32Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![shahryarjb](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/shahryarjb/32/39485_2.png) [@shahryarjb](https://forum.elixirforum.com/u/shahryarjb)\
**Post date:** [August 23, 2021, 10:36am UTC](https://forum.elixirforum.com/t/create-hs256-in-linux-command/41854/1 "2021-08-23T10:36:32Z")

</div>

Hello, I need to create `HS256` to save this for encryption, in this elixir I can do it with:

```elixir
JOSE.JWS.generate_key(%{"alg" => "HS256"}) |> JOSE.JWK.to_map |> elem(1)

```

but how can I do this in linux command? because I have a shell which creates docker image for me.

I see this page but I think this page result is different with elixir code

> <https://gist.github.com/indrayam/dd47bf6eef849a57c07016c0036f5207>

Thanks

---

<div class="post-metadata">

**Author:** ![kip](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/kip/32/1440_2.png) [@kip](https://forum.elixirforum.com/u/kip)\
**Post date:** [August 23, 2021, 11:15am UTC](https://forum.elixirforum.com/t/create-hs256-in-linux-command/41854/2 "2021-08-23T11:15:29Z")

</div>

I suggest [this](https://stackoverflow.com/questions/59002949/how-to-create-a-json-web-token-jwt-using-openssl-shell-commands) would appear to be doing what you ask using using `openssl`.

---

<div class="post-metadata">

**Author:** ![shahryarjb](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/shahryarjb/32/39485_2.png) [@shahryarjb](https://forum.elixirforum.com/u/shahryarjb)\
**Post date:** [August 23, 2021, 4:41pm UTC](https://forum.elixirforum.com/t/create-hs256-in-linux-command/41854/3 "2021-08-23T16:41:57Z")

</div>

Thanks, when I create the “k” with linux command:

```elixir
$(echo -n '{"alg":"HS256","typ":"JWT"}' | base64 | sed s/\+/-/g | sed 's/\//_/g' | sed -E s/=+$//)

```

this will be 36 characters, but when I use

```elixir
JOSE.JWS.generate_key(%{"alg" => "HS256"}) |> JOSE.JWK.to_map |> elem(1)
%{
  "alg" => "HS256",
  "k" => "4gOvXdZuoc9_iffdfDnZB2lsxhEePhUe16pFbQXX9Mc",
  "kty" => "oct",
  "use" => "sig"
}

```

it will be 43 characters, why are these difference between? what does mean `"k" =>` and how to create this?

😍

---

<div class="post-metadata">

**Author:** ![RudManusachi](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/rudmanusachi/32/36091_2.png) [@RudManusachi](https://forum.elixirforum.com/u/RudManusachi)\
**Post date:** [August 23, 2021, 9:29pm UTC](https://forum.elixirforum.com/t/create-hs256-in-linux-command/41854/4 "2021-08-23T21:29:19Z")

</div>

> [@shahryarjb](#):
>
> what does mean `"k" =>` and how to create this?

from [JWK Parameters for Symmetric Keys](https://tools.ietf.org/id/draft-jones-jose-json-private-and-symmetric-key-00.html#rfc.section.4.1)

> The k (key value) member contains the value of the symmetric (or other single-valued) key. It is represented as the base64url encoding of the octet sequence containing the key value.

JOSE adds that “k” field as a random string generated by: `crypto:strong_rand_bytes(Size)` (In case of `HS256` eventually it happens [here](https://github.com/potatosalad/erlang-jose/blob/master/src/jwk/jose_jwk_kty_oct.erl#L68-L69))

you can try:

```elixir
iex> {_, _, {_, key}, _} = :jose_jws_alg.generate_key({:oct, 32}, "HS256")
{:jose_jwk, :undefined,
 {:jose_jwk_kty_oct,
  <<228, 203, 21, 38, 196, 206, 135, 106, 22, 209, 53, 114, 53, 199, 255, 223,
    52, 25, 242, 56, 5, 215, 65, 135, 13, 217, 14, 38, 235, 214, 135, 165>>},

iex> :jose_jwa_base64url.encode(key)
"5MsVJsTOh2oW0TVyNcf_3zQZ8jgF10GHDdkOJuvWh6U"

# which is basically the same as
iex> Base.url_encode64(key, padding: false)
"5MsVJsTOh2oW0TVyNcf_3zQZ8jgF10GHDdkOJuvWh6U"

```

So in bash probably something like

```elixir
dd if=/dev/urandom bs=32 count=1 | base64 | sed 's/+/-/g; s/\//_/g; s/=//g'

```

Could generate you similarly random password to put into `"k"` field of the map
