目次
How crypt() Function works in PHP?
PHP crypt() Constants
1. CRYPT_STD_DES
2. CRYPT_EXT_DES
3. CRYPT_MD5
4. CRYPT_BLOWFISH
6. CRYPT_SHA512
Conclusion – PHP crypt()

PHP crypt()

Aug 29, 2024 pm 12:53 PM
php

PHP crypt() function is a part of PHP string references whose main function is to return a hashed string using some special algorithms. PHP crypt() function is associated with the algorithms like DES, Blowfish or MD5 algorithms for its overall network and cryptographic encryption and decryption of string being passed from the crypt() function. Crypt() function vary from one function to another function in a way that behavior gets transformed accordingly to different operating system. It checks for all the available algorithms or if any need is there to install new algorithm for encryption.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

Syntax:

crypt(str,salt)
ログイン後にコピー
  • The syntax flows in a way that crypt function is a function which passes string and salt as a parameter.
  • str is a required string which specifies the string needs to be hashed in a one-way fashion.
  • salt is another optional parameter where a salt string to base the hashing on.
  • But PHP with versions of 5+ have salt as a parameter to pass carrying different algorithms like blowfish nodes, standard DES algorithm, extended DES and the blowfish algorithm.

How crypt() Function works in PHP?

  • Crypt is a one-way string hashing.
  • Salt as a parameter gets passed to crypt() function is optional. But if salt parameter is not passed then that key will be considered as weak hash.
  • To perform a good security, it is important to pass a very strong hashed key. A strong hash which is used to generate a strong salt and then applies a proper rounds of hash key rotation in a round robin fashion uses a password_hash.
  • Using a password_hash is a recommended method in a way it acts as a wrapper on top of the algorithm and makes the algorithm compatible with one-way hashing string.
  • As mentioned, these crypt() function works on an operating system which support many different operating systems which acts as a key.
  • The salt parameter passed with the string to the crypt function triggers to the salt algorithm. These functionalities work with the 4 version of PHP but the versions more than 5 .
  • PHP has a capability of creating an auto-generated key if no DES salt parameter is present. If in case it is related to the twelve-character key, then MD5 algorithm will create a one-way hashed key.

PHP crypt() Constants

There are following constants for crypt() function which have some values set as 0 or 1 depending on whether the given type of crypt constants is available.

1. CRYPT_STD_DES

First constant which has a two-character salt parameter passed from the alphabet. Also, supports for the values supporting the digits with numbers of uppercase 0-9 and alphabets with upper case A-Z and lower case alphabets like a-z. crypt function will get failed if some invalid characters will be used while passing the salt parameter.

Example: A program to illustrate CRYPT_STD_DES constant.

Code:

<?php
if (CRYPT_STD_DES == 1)
{
echo "Standard DES: ".crypt('educba','string')."\n";
}
else
{
echo "Do not support standard DES.\n";
}
?>
ログイン後にコピー

Output:

PHP crypt()

2. CRYPT_EXT_DES

This constant is a type of extension to the DES based hashing function. If the salt parameter being passed has a nine character string followed by the parameter of 4 bytes, then only the algorithm will get satisfied.

Example: A program to illustrate CRYPT_EXT_DES constant.

Code:

<?php
if (CRYPT_EXT_DES == 1)
{
echo "Extended DES: ".crypt('anu','_D8..dutta')."\n";
}
else
{
echo "It do not support for Extended DES.\n";
}
?>
ログイン後にコピー

Output:

PHP crypt()

3. CRYPT_MD5

This constant MD5 works with hashing function including crypt parallelly with a character of salt parameter having a length of twelve character.

Example: A program to illustrate CRYPT_MD5 constant.

Code:

<?php
if (CRYPT_MD5 == 1)
{
echo "MD5: ".crypt('mansi','$1$trying$')."\n";
}
else
{
echo "Do not support for MD5.\n";
}
?>
ログイン後にコピー

Output:

PHP crypt()

4. CRYPT_BLOWFISH

This constant support for the function with salt parameter containing some cost parameter ranging from “$” to 22. If the parameter value does not lie within the specified range, then it will return a string of zero-length. The cost parameter is considered as twice if the base of logarithmic value for the illustrated blowfish-based hashing algorithm. PHP versions of 5 and above supports for the given constant.

Example: A program to illustrate CRYPT_BLOWFISH constant.

Code:

<?php
if (CRYPT_BLOWFISH == 1)
{
echo "Blowfish: ".crypt('sunrise','$1b$08$mkstringexforsaltparam$')."\n";
}
else
{
echo "It do not support for Blowfish.\n";
}
?>
ログイン後にコピー

Output:

PHP crypt()

5. CRYPT_SHA256

SHA-256 is a constant which is part of the algorithm with a hash value of sixteen character. If in case the string gets started with the round of $N hen it indicates number of times hashed function gets called and executes with the optimization and cost factor like Blowfish algorithm. Also, if the selection of numbers does not lie outside the range then the next value of the range will get approximation to the closest value of the range.

Example: A program to illustrate CRYPT_SHA256 constant.

Code:

<?php
if (CRYPT_SHA256 == 1)
{
echo "SHA-256: ".crypt('sunfeast','$8$rounds=8000$examplestringforsaltofsalt$')."\n"; }
else
{
echo "It do not support for CRYPT_SHA256.\n";
}
?>
ログイン後にコピー

Output:

PHP crypt()

6. CRYPT_SHA512

This is a constant which is prefixed with some value like 6$. If the round function gets started with the value of taken round of number of salts , then it points for the optimized value same as Blowfish function. Also, it can be said that behavior of the constant is same as SHA-256 constant with just some mere differences.

Example: A program to illustrate CRYPT_SHA512 constant.

Code:

<?php
if (CRYPT_SHA512 == 1)
{
echo "SHA-512: ".crypt('things','$9$rounds=9000$xamplestringof90salt$');
}
else
{
echo " It donot support for CRYPT_SHA512 .";
}
?>
ログイン後にコピー

Output:

PHP crypt()

Note: The system with version of 5.3.0 contains implementation of its own type and will use that implementation if that system lacks the install then it will look for the constant and its related algorithm for self-installation and implementation.

Conclusion – PHP crypt()

PHP crypt() function can encrypt the hashed string and is a one directional cryptographic method supporting the mentioned algorithm and it specifically supports for encryption not for decryption that is why it is named as one-directional algorithm.

以上がPHP crypt()の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。

ホットAIツール

Undresser.AI Undress

Undresser.AI Undress

リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover

AI Clothes Remover

写真から衣服を削除するオンライン AI ツール。

Undress AI Tool

Undress AI Tool

脱衣画像を無料で

Clothoff.io

Clothoff.io

AI衣類リムーバー

Video Face Swap

Video Face Swap

完全無料の AI 顔交換ツールを使用して、あらゆるビデオの顔を簡単に交換できます。

ホットツール

メモ帳++7.3.1

メモ帳++7.3.1

使いやすく無料のコードエディター

SublimeText3 中国語版

SublimeText3 中国語版

中国語版、とても使いやすい

ゼンドスタジオ 13.0.1

ゼンドスタジオ 13.0.1

強力な PHP 統合開発環境

ドリームウィーバー CS6

ドリームウィーバー CS6

ビジュアル Web 開発ツール

SublimeText3 Mac版

SublimeText3 Mac版

神レベルのコード編集ソフト(SublimeText3)

PHP対Python:違いを理解します PHP対Python:違いを理解します Apr 11, 2025 am 12:15 AM

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHPは、シンプルな構文と高い実行効率を備えたWeb開発に適しています。 2。Pythonは、簡潔な構文とリッチライブラリを備えたデータサイエンスと機械学習に適しています。

PHP:Web開発の重要な言語 PHP:Web開発の重要な言語 Apr 13, 2025 am 12:08 AM

PHPは、サーバー側で広く使用されているスクリプト言語で、特にWeb開発に適しています。 1.PHPは、HTMLを埋め込み、HTTP要求と応答を処理し、さまざまなデータベースをサポートできます。 2.PHPは、ダイナミックWebコンテンツ、プロセスフォームデータ、アクセスデータベースなどを生成するために使用され、強力なコミュニティサポートとオープンソースリソースを備えています。 3。PHPは解釈された言語であり、実行プロセスには語彙分析、文法分析、編集、実行が含まれます。 4.PHPは、ユーザー登録システムなどの高度なアプリケーションについてMySQLと組み合わせることができます。 5。PHPをデバッグするときは、error_reporting()やvar_dump()などの関数を使用できます。 6. PHPコードを最適化して、キャッシュメカニズムを使用し、データベースクエリを最適化し、組み込み関数を使用します。 7

PHPとPython:2つの一般的なプログラミング言語を比較します PHPとPython:2つの一般的なプログラミング言語を比較します Apr 14, 2025 am 12:13 AM

PHPとPythonにはそれぞれ独自の利点があり、プロジェクトの要件に従って選択します。 1.PHPは、特にWebサイトの迅速な開発とメンテナンスに適しています。 2。Pythonは、データサイエンス、機械学習、人工知能に適しており、簡潔な構文を備えており、初心者に適しています。

アクション中のPHP:実際の例とアプリケーション アクション中のPHP:実際の例とアプリケーション Apr 14, 2025 am 12:19 AM

PHPは、電子商取引、コンテンツ管理システム、API開発で広く使用されています。 1)eコマース:ショッピングカート機能と支払い処理に使用。 2)コンテンツ管理システム:動的コンテンツの生成とユーザー管理に使用されます。 3)API開発:RESTFUL API開発とAPIセキュリティに使用されます。パフォーマンスの最適化とベストプラクティスを通じて、PHPアプリケーションの効率と保守性が向上します。

PHPの永続的な関連性:それはまだ生きていますか? PHPの永続的な関連性:それはまだ生きていますか? Apr 14, 2025 am 12:12 AM

PHPは依然として動的であり、現代のプログラミングの分野で重要な位置を占めています。 1)PHPのシンプルさと強力なコミュニティサポートにより、Web開発で広く使用されています。 2)その柔軟性と安定性により、Webフォーム、データベース操作、ファイル処理の処理において顕著になります。 3)PHPは、初心者や経験豊富な開発者に適した、常に進化し、最適化しています。

PHPおよびPython:さまざまなパラダイムが説明されています PHPおよびPython:さまざまなパラダイムが説明されています Apr 18, 2025 am 12:26 AM

PHPは主に手順プログラミングですが、オブジェクト指向プログラミング(OOP)もサポートしています。 Pythonは、OOP、機能、手続き上のプログラミングなど、さまざまなパラダイムをサポートしています。 PHPはWeb開発に適しており、Pythonはデータ分析や機械学習などのさまざまなアプリケーションに適しています。

PHPおよびPython:コードの例と比較 PHPおよびPython:コードの例と比較 Apr 15, 2025 am 12:07 AM

PHPとPythonには独自の利点と短所があり、選択はプロジェクトのニーズと個人的な好みに依存します。 1.PHPは、大規模なWebアプリケーションの迅速な開発とメンテナンスに適しています。 2。Pythonは、データサイエンスと機械学習の分野を支配しています。

PHP対その他の言語:比較 PHP対その他の言語:比較 Apr 13, 2025 am 12:19 AM

PHPは、特に迅速な開発や動的なコンテンツの処理に適していますが、データサイエンスとエンタープライズレベルのアプリケーションには良くありません。 Pythonと比較して、PHPはWeb開発においてより多くの利点がありますが、データサイエンスの分野ではPythonほど良くありません。 Javaと比較して、PHPはエンタープライズレベルのアプリケーションでより悪化しますが、Web開発により柔軟性があります。 JavaScriptと比較して、PHPはバックエンド開発により簡潔ですが、フロントエンド開発のJavaScriptほど良くありません。

See all articles