Elixir::Base.encode16 use ruby method (hexdigest, unpack)
https://www.rubydoc.info/gems/elixir.rb/0.1.0/Elixir%2FBase.encode16
https://apidock.com/ruby/String/unpack
https://ruby-doc.org/stdlib-2.4.0/libdoc/openssl/rdoc/OpenSSL/HMAC.html
Method: Elixir::Base.encode16
- Defined in:
- lib/elixir/base.rb
permalink.encode16(data, char_case: :upper) ⇒ Object
63 64 65 66 67 |
# File 'lib/elixir/base.rb', line 63
def encode16 data, char_case: :upper
encoded = data.unpack('H*').first
char_case == :upper ? encoded.upcase : encoded
end
|
Elixir method:
secret_key = SecureRandom.urlsafe_base64(64) def secret_hash(token) do :crypto.hmac( :sha256, Application.get_env(:broker, BrokerWeb.Endpoint)[:secret_key_base], secret_key ) |> Base.encode16() end
这个要把 secret_key 变成一个 hash, 使用ruby方法的实现。
@secret_hash = OpenSSL::HMAC.hexdigest("SHA256", ENV['TOKEN_SECRET_KEY'], secret_key)
就这一行代码就可以了, hexdigest 就等于是 digest再调用 unpack("H*")方法。