#目的
Elixirで利用できる、safetybox(暗号化ライブラリ)を使って文字を暗号化してみる。#実行環境
OS: Windows8.1Erlang: Eshell V6.4, OTP-Version 17.5
Elixir: v1.0.4
Safetybox: v0.1.2
#始める前に
READMEの通りに実行しただけ。#目次
1. プロジェクト生成2. mix.exs
3. iexから利用
4. 暗号化と認証(仮)を行うソースコード
##1. プロジェクト生成
>mix new safetybox_sample>cd safetybox_sample
>mix test
##2. mix.exs
以下の記述を依存関係に追加。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defp deps do | |
[ { :safetybox, "~> 0.1" } ] | |
end |
>mix compile
##3. iexから利用
>iex -S mixiex(1)> pwd = Safetybox.encrypt("helloworld")
"fc5e038d38a57032085441e7fe7010b0"
iex(2)> Safetybox.is_decrypted("helloelixir", pwd)
false
iex(3)> Safetybox.is_decrypted("helloworld", pwd)
true
##4. 暗号化と認証(仮)を行うソースコード
今後利用する予定があるのでサンプルを作成しておく。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule SafetyboxSample do | |
def encrypt(password) do | |
Safetybox.encrypt(password) | |
end | |
def authentication(_userid, _user_name, password) do | |
encrypt_password = "" # DBからユーザデータを取得する処理が入る | |
Safetybox.is_decrypted(password, encrypt_password) | |
end | |
end |
>iex -S mix
iex(1)> enc_pass = SafetyboxSample.encrypt("helloworld")
"fc5e038d38a57032085441e7fe7010b0"
iex(2)> SafetyboxSample.authentication(3, "hoge", "helloworld")
false
こんな感じで利用する予定。
#管理人の独り言~
Rails Tutorial for Phoenixで暗号化を利用したいがためにやった。本当はこちらのライブラリを使いたかったのだが、
GCCがないと駄目みたいなので・・・変更しました。
Github: Comeonin (bcryptとpbkdf2_sha512をサポートしてくれている)
しかし、すぐ使うならこちらのライブラリで十分です。
(外部公開はしないし、処理を入れたいだけ・・・ですし)
Comeoninの方はLinuxに環境を移したらですね。
bcryptサポートしてますし使い勝手が良さげ。
大した内容はないけど、プロジェクトはGithubへアップしてます。
Github: https://github.com/darui00kara/phoenix_tutorial/tree/master/safetybox_sample