すべてのプロダクト
Search
ドキュメントセンター

:暗号アルゴリズム関数

最終更新日:Sep 20, 2024

このトピックでは、暗号アルゴリズム関数の構文、説明、パラメーター、および戻り値について説明します。 このトピックでは、これらの関数の例も示します。

aes_new

この関数の詳細を次の表に示します。

項目

説明

構文

aes_new(config)

説明

後続の暗号化および復号化操作に使用されるAdvanced Encryption Standard (AES) オブジェクトを作成します。 暗号化を実行するには、aes_enc() 関数を呼び出します。 復号化を実行するには、aes_dec() 関数を呼び出します。

パラメーター

configパラメーターはディクショナリ型で、次の引数が含まれます。

  • key: 文字列型のキーです。 This parameter is required.

  • salt: 文字列型のsalt値。 このパラメーターはオプションです。

  • cipher_len: キーの長さ。 This parameter is required. 有効な値: 128、192、256

  • cipher_mode: 暗号化または復号化に使用されるモード。 This parameter is required. 有効な値: ecb、cbc、ctr、cfb、およびofb。

  • iv: 文字列型である初期ベクトル。 このパラメーターはオプションです。

戻り値

関数が成功すると、AESオブジェクト (辞書型) が返されます。 それ以外の場合、falseが返されます。

例:

aes_conf = []                                                                                                                                                                         
plaintext = ''                                                                                                                                                                        
if and($http_mode, eq($http_mode, 'ecb-128')) {                                                                                                                                       
  set(aes_conf, 'key', 'ab8bfd9f-a1af-4ba2-bbb0-1ee520e3d8bc')                                                                                                                      
  set(aes_conf, 'salt', '1234567890')                                                                                                                                               
  set(aes_conf, 'cipher_len', 128)                                                                                                                                                  
  set(aes_conf, 'cipher_mode', 'ecb')                                                                                                                                               
  plaintext = 'hello aes ecb-128'                                                                                                                                                   
}                                                                                                                                                                                     
if and($http_mode, eq($http_mode, 'cbc-256')) {                                                                                                                                       
  set(aes_conf, 'key', '146ebcc8-392b-4b3a-a720-e7356f62')                                                                                                                          
  set(aes_conf, 'cipher_len', 256)                                                                                                                                                  
  set(aes_conf, 'cipher_mode', 'cbc')                                                                                                                                               
  set(aes_conf, 'iv', '0123456789abcdef')                                                                                                                                           
  plaintext = 'hello aes cbc-256'                                                                                                                                                   
}                                                                                                                                                                                     
if and($http_mode, eq($http_mode, 'ofb-256')) {                                                                                                                                       
  set(aes_conf, 'key', '146ebcc8-392b-4b3a-a720-e7356f62')                                                                                                                          
  set(aes_conf, 'cipher_len', 256)                                                                                                                                                  
  set(aes_conf, 'cipher_mode', 'ofb')                                                                                                                                               
  set(aes_conf, 'iv', tochar(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0))                                                                                                                      
  plaintext = 'hello aes ofb-256'                                                                                                                                                   
}                                                                                                                                                                                     

aes_obj = aes_new(aes_conf)                                                                                                                                                           
if not(aes_obj) {                                                                                                                                                                     
  say(concat('aes obj failed'))                                                                                                                                                     
  exit(400)                                                                                                                                                                         
}                                                                                                                                                                                     

ciphertext = aes_enc(aes_obj, plaintext)                                                                                                                                              
plaintext_reverse = aes_dec(aes_obj, ciphertext)                                                                                                                                      

say(concat('plain: ', plaintext))                                                                                                                                                     
say(concat('cipher: ', tohex(ciphertext)))                                                                                                                                            
say(concat('plain_reverse: ', plaintext_reverse))                                                                                                                                     

if ne(plaintext, plaintext_reverse) {                                                                                                                                                 
  say('plaintext ~= plaintext_reverse')                                                                                                                                            
  exit(400)                                                                                                                                                                        
}

aes_enc

この関数の詳細を次の表に示します。

項目

説明

構文

aes_enc(o, s)

説明

AES暗号化アルゴリズムを使用してデータを暗号化します。

パラメーター

  • s: 暗号化されるプレーンテキスト。

  • o: aes_new関数によって返されるAESオブジェクト。

戻り値

sパラメーターで指定されたテキストが暗号化された後の暗号文を返します。

例:

aes_conf = []                                                                                                                                                                         
plaintext = ''                                                                                                                                                                        
if and($http_mode, eq($http_mode, 'ecb-128')) {                                                                                                                                       
  set(aes_conf, 'key', 'ab8bfd9f-a1af-4ba2-bbb0-1ee520e3d8bc')                                                                                                                      
  set(aes_conf, 'salt', '1234567890')                                                                                                                                               
  set(aes_conf, 'cipher_len', 128)                                                                                                                                                  
  set(aes_conf, 'cipher_mode', 'ecb')                                                                                                                                               
  plaintext = 'hello aes ecb-128'                                                                                                                                                   
}                                                                                                                                                                                     
if and($http_mode, eq($http_mode, 'cbc-256')) {                                                                                                                                       
  set(aes_conf, 'key', '146ebcc8-392b-4b3a-a720-e7356f62')                                                                                                                          
  set(aes_conf, 'cipher_len', 256)                                                                                                                                                  
  set(aes_conf, 'cipher_mode', 'cbc')                                                                                                                                               
  set(aes_conf, 'iv', '0123456789abcdef')                                                                                                                                           
  plaintext = 'hello aes cbc-256'                                                                                                                                                   
}                                                                                                                                                                                     
if and($http_mode, eq($http_mode, 'ofb-256')) {                                                                                                                                       
  set(aes_conf, 'key', '146ebcc8-392b-4b3a-a720-e7356f62')                                                                                                                          
  set(aes_conf, 'cipher_len', 256)                                                                                                                                                  
  set(aes_conf, 'cipher_mode', 'ofb')                                                                                                                                               
  set(aes_conf, 'iv', tochar(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0))                                                                                                                      
  plaintext = 'hello aes ofb-256'                                                                                                                                                   
}                                                                                                                                                                                     

aes_obj = aes_new(aes_conf)                                                                                                                                                           
if not(aes_obj) {                                                                                                                                                                     
  say(concat('aes obj failed'))                                                                                                                                                     
  exit(400)                                                                                                                                                                         
}                                                                                                                                                                                     

ciphertext = aes_enc(aes_obj, plaintext)                                                                                                                                              
plaintext_reverse = aes_dec(aes_obj, ciphertext)                                                                                                                                      

say(concat('plain: ', plaintext))                                                                                                                                                     
say(concat('cipher: ', tohex(ciphertext)))                                                                                                                                            
say(concat('plain_reverse: ', plaintext_reverse))                                                                                                                                     

if ne(plaintext, plaintext_reverse) {                                                                                                                                                 
  say('plaintext ~= plaintext_reverse')                                                                                                                                            
  exit(400)                                                                                                                                                                        
}

aes_dec

この関数の詳細を次の表に示します。

項目

説明

構文

aes_dec(o, s)

説明

AES暗号化アルゴリズムを使用してデータを復号化します。

パラメーター

  • s: 復号化される暗号文。

  • o: aes_new関数によって返されるAESオブジェクト。

戻り値

sパラメーターで指定されたテキストが復号化された後のプレーンテキストを返します。

例:

aes_conf = []                                                                                                                                                                         
plaintext = ''                                                                                                                                                                        
if and($http_mode, eq($http_mode, 'ecb-128')) {                                                                                                                                       
  set(aes_conf, 'key', 'ab8bfd9f-a1af-4ba2-bbb0-1ee520e3d8bc')                                                                                                                      
  set(aes_conf, 'salt', '1234567890')                                                                                                                                               
  set(aes_conf, 'cipher_len', 128)                                                                                                                                                  
  set(aes_conf, 'cipher_mode', 'ecb')                                                                                                                                               
  plaintext = 'hello aes ecb-128'                                                                                                                                                   
}                                                                                                                                                                                     
if and($http_mode, eq($http_mode, 'cbc-256')) {                                                                                                                                       
  set(aes_conf, 'key', '146ebcc8-392b-4b3a-a720-e7356f62')                                                                                                                          
  set(aes_conf, 'cipher_len', 256)                                                                                                                                                  
  set(aes_conf, 'cipher_mode', 'cbc')                                                                                                                                               
  set(aes_conf, 'iv', '0123456789abcdef')                                                                                                                                           
  plaintext = 'hello aes cbc-256'                                                                                                                                                   
}                                                                                                                                                                                     
if and($http_mode, eq($http_mode, 'ofb-256')) {                                                                                                                                       
  set(aes_conf, 'key', '146ebcc8-392b-4b3a-a720-e7356f62')                                                                                                                          
  set(aes_conf, 'cipher_len', 256)                                                                                                                                                  
  set(aes_conf, 'cipher_mode', 'ofb')                                                                                                                                               
  set(aes_conf, 'iv', tochar(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0))                                                                                                                      
  plaintext = 'hello aes ofb-256'                                                                                                                                                   
}                                                                                                                                                                                     

aes_obj = aes_new(aes_conf)                                                                                                                                                           
if not(aes_obj) {                                                                                                                                                                     
  say(concat('aes obj failed'))                                                                                                                                                     
  exit(400)                                                                                                                                                                         
}                                                                                                                                                                                     

ciphertext = aes_enc(aes_obj, plaintext)                                                                                                                                              
plaintext_reverse = aes_dec(aes_obj, ciphertext)                                                                                                                                      

say(concat('plain: ', plaintext))                                                                                                                                                     
say(concat('cipher: ', tohex(ciphertext)))                                                                                                                                            
say(concat('plain_reverse: ', plaintext_reverse))                                                                                                                                     

if ne(plaintext, plaintext_reverse) {                                                                                                                                                 
  say('plaintext ~= plaintext_reverse')                                                                                                                                            
  exit(400)                                                                                                                                                                        
}

sha1

この関数の詳細を次の表に示します。

項目

説明

構文

sha1(s)

説明

SHA-1値を計算します。

パラメーター

s: CRCダイジェストを計算する文字列。

戻り値

バイナリ形式でSHA-1値を返します。

例:

digest = sha1('hello sha')                                                                                                                                                        
say(concat('sha1:', tohex(digest)))

Output:
sha1:853789bc783a6b573858b6cc9f913afe82962956

sha2

この関数の詳細を次の表に示します。

項目

説明

構文

sha2(s, l)

説明

SHA-2値を計算します。

パラメーター

  • s: CRCダイジェストを計算する文字列。

  • l: SHA-2値の長さ。 有効な値: 224、256、384、512

戻り値

SHA-2値をバイナリ形式で返します。

例:

digest = sha2('hello sha2', 224)
say(concat('sha2-224:', tohex(digest)))

digest = sha2('hello sha2', 256)
say(concat('sha2-256:', tohex(digest)))

digest = sha2('hello sha2', 384)
say(concat('sha2-384:', tohex(digest)))

digest = sha2('hello sha2', 512)
say(concat('sha2-512:', tohex(digest)))

Output:
sha2-224:b24b7effcf53ce815ee7eb73c7382613aba1c334e2a1622655362927
sha2-256:af0425cee23c236b326ed1f008c9c7c143a611859a11e87d66d0a4c3217c7792
sha2-384:bebbdde9efabd4b9cf90856cf30e0b024dd13177d9367d2dcf8d7a04e059f92260f16b21e261358c2271be32086ef35b
sha2-512:a1d1aef051c198c0d26bc03500c177a315fa248cea815e04fbb9a75e5be5061617daab311c5e3d0b215dbfd4e83e73f23081242b0143dcdfce5cd92ec51394f7

hmac

この関数の詳細を次の表に示します。

項目

説明

構文

hmac(k, s, v)

説明

HMAC値を計算します。

パラメーター

  • k: アルゴリズムのキー。

  • s: HMAC値を計算する文字列。

  • v: アルゴリズムのバージョン。 有効な値: md5、sha256、およびsha512。

戻り値

対応するアルゴリズムを使用して、バイナリ形式でHMAC値を返します。

例:

k = '146ebcc8-392b-4b3a-a720-e7356f62f87b'
v = 'hello mac'
say(concat('hmac(md5): ', tohex(hmac(k, v, 'md5'))))
say(concat('hmac(sha1): ', tohex(hmac(k, v, 'sha1'))))
say(concat('hmac(sha256): ', tohex(hmac(k, v, 'sha256'))))
say(concat('hmac(sha512): ', tohex(hmac(k, v, 'sha512'))))
say(concat('hmac_sha1(): ', tohex(hmac_sha1(k, v))))

Output:
hmac(md5): 358cbfca8ad663b547c83748de2ea778
hmac(sha1): 5555633cef48c3413b68f9330e99357df1cc3d93
hmac(sha256): 7a494543cad3b92ce1e7c4bbc86a8f5212b53e4d661f7830f455847540a85771
hmac(sha512): 59d7c07996ff675b45bd5fd40a6122bb5f40f597357a9b4a9e29da6f5c7cb806798c016fe09cb46457b6df9717d26d0af19896f72eaf4296be03e3681fea59ad
hmac_sha1(): 5555633cef48c3413b68f9330e99357df1cc3d93

hmac_sha1

この関数の詳細を次の表に示します。

項目

説明

構文

hmac_sha1(k, s)

説明

HMAC-SHA-1値を計算します。

パラメーター

  • s: HMAC-SHA-1値を計算する文字列。

  • k: HMAC-SHA-1キー。

戻り値

HMAC-SHA-1値をバイナリ形式で返します。

例:

k = '146ebcc8-392b-4b3a-a720-e7356f62f87b'
v = 'hello mac'
say(concat('hmac(md5): ', tohex(hmac(k, v, 'md5'))))
say(concat('hmac(sha1): ', tohex(hmac(k, v, 'sha1'))))
say(concat('hmac(sha256): ', tohex(hmac(k, v, 'sha256'))))
say(concat('hmac(sha512): ', tohex(hmac(k, v, 'sha512'))))
say(concat('hmac_sha1(): ', tohex(hmac_sha1(k, v))))

Output:
hmac(md5): 358cbfca8ad663b547c83748de2ea778
hmac(sha1): 5555633cef48c3413b68f9330e99357df1cc3d93
hmac(sha256): 7a494543cad3b92ce1e7c4bbc86a8f5212b53e4d661f7830f455847540a85771
hmac(sha512): 59d7c07996ff675b45bd5fd40a6122bb5f40f597357a9b4a9e29da6f5c7cb806798c016fe09cb46457b6df9717d26d0af19896f72eaf4296be03e3681fea59ad
hmac_sha1(): 5555633cef48c3413b68f9330e99357df1cc3d93

md5

この関数の詳細を次の表に示します。

項目

説明

構文

md5(s)

説明

MD5ダイジェストを計算します。

パラメーター

s: CRCダイジェストを計算する文字列。

戻り値

MD5値を16進形式で返します。

例:

say(concat('md5: ', md5('hello md5')))

Output:
md5: 741fc6b1878e208346359af502dd11c5

md5_bin

この関数の詳細を次の表に示します。

項目

説明

構文

md5_bin(s)

説明

MD5ダイジェストを計算します。

パラメーター

s: CRCダイジェストを計算する文字列。

戻り値

バイナリ形式でMD5ダイジェストを返します。

例:

say(concat('md5_bin: ', tohex(md5_bin('hello md5'))))

Output:
md5_bin: 741fc6b1878e208346359af502dd11c5