ハッシュ関数
hashモジュールは、暗号学的ハッシュ、HMAC値、PBKDF2で導出した鍵、非暗号学的なFNV-1ハッシュを計算します。このページは独立した呼び出しを示すAPIリファレンスです。リテラル入力は正常な使用例を示しています。データ、シークレット、パスワード、ソルトをアプリケーションから受け取る場合は、結果を使用する前に、文書化された2番目のerror戻り値を取得して処理してください。
ハッシュは暗号化ではなく、エントロピーの低い入力を秘匿しません。パスワード、HMAC鍵、導出鍵、シークレットに依存する生のダイジェストをログに記録しないでください。新しいメッセージ認証設計にはHMAC-SHA256またはHMAC-SHA512を使用し、パスワード検証子には一意なランダムソルトを指定したPBKDF2を使用してください。
ロード
local hash = require("hash")
暗号学的ハッシュ
MD5
MD5には衝突耐性がありません。セキュリティ上の判断には使用せず、MD5を必要とするプロトコルとの互換性のためだけに使用してください。
local hex = hash.md5("data")
local raw = hash.md5("data", true)
| パラメータ | 型 | 説明 |
|---|---|---|
data |
string | ハッシュするデータ |
raw |
boolean? | hexの代わりに生バイトを返す |
戻り値: string, error
SHA-1
SHA-1には衝突耐性がありません。セキュリティ上の判断には使用せず、SHA-1を必要とするプロトコルとの互換性のためだけに使用してください。
local hex = hash.sha1("data")
local raw = hash.sha1("data", true)
| パラメータ | 型 | 説明 |
|---|---|---|
data |
string | ハッシュするデータ |
raw |
boolean? | hexの代わりに生バイトを返す |
戻り値: string, error
SHA-256
local hex = hash.sha256("data")
local raw = hash.sha256("data", true)
| パラメータ | 型 | 説明 |
|---|---|---|
data |
string | ハッシュするデータ |
raw |
boolean? | hexの代わりに生バイトを返す |
戻り値: string, error
SHA-512
local hex = hash.sha512("data")
local raw = hash.sha512("data", true)
| パラメータ | 型 | 説明 |
|---|---|---|
data |
string | ハッシュするデータ |
raw |
boolean? | hexの代わりに生バイトを返す |
戻り値: string, error
HMAC
HMAC-MD5
HMAC-MD5は、それを必要とするプロトコルとの互換性のためだけに使用してください。新しい設計ではHMAC-SHA256またはHMAC-SHA512を推奨します。
local hex = hash.hmac_md5("message", "secret")
local raw = hash.hmac_md5("message", "secret", true)
| パラメータ | 型 | 説明 |
|---|---|---|
data |
string | 認証するメッセージ |
secret |
string | 秘密鍵 |
raw |
boolean? | hexの代わりに生バイトを返す |
戻り値: string, error
HMAC-SHA1
HMAC-SHA1は、それを必要とするプロトコルとの互換性のためだけに使用してください。新しい設計ではHMAC-SHA256またはHMAC-SHA512を推奨します。
local hex = hash.hmac_sha1("message", "secret")
local raw = hash.hmac_sha1("message", "secret", true)
| パラメータ | 型 | 説明 |
|---|---|---|
data |
string | 認証するメッセージ |
secret |
string | 秘密鍵 |
raw |
boolean? | hexの代わりに生バイトを返す |
戻り値: string, error
HMAC-SHA256
local hex = hash.hmac_sha256("message", "secret")
local raw = hash.hmac_sha256("message", "secret", true)
| パラメータ | 型 | 説明 |
|---|---|---|
data |
string | 認証するメッセージ |
secret |
string | 秘密鍵 |
raw |
boolean? | hexの代わりに生バイトを返す |
戻り値: string, error
HMAC-SHA512
local hex = hash.hmac_sha512("message", "secret")
local raw = hash.hmac_sha512("message", "secret", true)
| パラメータ | 型 | 説明 |
|---|---|---|
data |
string | 認証するメッセージ |
secret |
string | 秘密鍵 |
raw |
boolean? | hexの代わりに生バイトを返す |
戻り値: string, error
非暗号学的ハッシュ
FNV-1 32-bit
ハッシュテーブルとパーティショニング用の高速ハッシュです。
local n = hash.fnv32("data")
| パラメータ | 型 | 説明 |
|---|---|---|
data |
string | ハッシュするデータ |
戻り値: number, error
FNV-1 64-bit
衝突を減らすための大きな出力を持つ高速ハッシュです。
local n = hash.fnv64("data")
| パラメータ | 型 | 説明 |
|---|---|---|
data |
string | ハッシュするデータ |
戻り値: number, error
鍵導出
PBKDF2
local key, err = hash.pbkdf2(password, salt, iterations, key_length)
local key, err = hash.pbkdf2(password, salt, iterations, key_length, "sha512")
| パラメータ | 型 | 説明 |
|---|---|---|
password |
string | パスワード/パスフレーズ(空でないこと) |
salt |
string | ソルト値(空でないこと) |
iterations |
integer | 反復回数(1〜10,000,000) |
key_length |
integer | 生成する鍵の長さ(バイト) |
hash |
string? | sha256またはsha512(デフォルト: sha256) |
戻り値: string, error(生の鍵バイト列)
エラー
| 条件 | 種別 | 再試行可能 |
|---|---|---|
| 入力が文字列でない | errors.INVALID |
no |
| シークレットが文字列でない(HMAC) | errors.INVALID |
no |
| パスワード/ソルトが空、反復回数が0以下または過大、非対応のハッシュ(PBKDF2) | errors.INVALID |
no |
エラーの処理についてはエラー処理を参照。
エラーの扱いについては、エラー処理を参照してください。