search
HomeBackend DevelopmentPHP TutorialNew features in PHP 8: Added verification and signing
New features in PHP 8: Added verification and signingMar 27, 2024 am 08:21 AM
verifysignphp new featuresstandard library

PHP 8 新特性:增加了验证和签名

PHP 8 is the latest version of PHP, bringing more convenience and functionality to programmers. This version has a special focus on security and performance, and one of the noteworthy new features is the addition of verification and signing capabilities. In this article, we'll take a closer look at these new features and their uses.

Verification and signature are very important security concepts in computer science. They are often used to ensure that the data transmitted is complete and authentic. Verification and signatures become especially important when dealing with online transactions and sensitive information, because if someone is able to tamper with the data, it could cause unpredictable harm to the company and customers.

In PHP 8, there are two new extensions for handling verification and signing: sodium and openssl. These extensions make it easier to use modern cryptography and security protocols in PHP.

sodium extension

Sodium is a modern cryptography library that provides API access to many cryptographic algorithms and security functions. It has become the library of choice for developers working with secure cryptography because it is based on state-of-the-art cryptography standards such as Curve25519 and Chacha20.

In PHP 7.2, the Sodium extension was added to the PHP standard library and became an optional extension in PHP 7.2. In PHP 8, it has become the default extension.

Use the sodium extension for verification

The sodium extension provides a function called sodium_crypto_sign_detached(), which can be used to sign data. The signature generates a specific string that can be used to verify whether the data has been tampered with.

The following is an example of signing and verifying using the sodium_crypto_sign_detached() function:

<?php
$data = "hello world";
$keys = sodium_crypto_sign_keypair();

$signature = sodium_crypto_sign_detached($data, $keys['secret']);

if (sodium_crypto_sign_verify_detached($signature, $data, $keys['public'])) {
   echo "签名有效";
} else {
   echo "签名无效";
}

In this example, we first generate a pair of public and private keys. Then use the $sodium_crypto_sign_detached() function to sign the string and store the signature result in the $signature variable. Finally, use the $sodium_crypto_sign_verify_detached() function to verify that the signature is valid.

If the signature is valid, then "Signature is valid" will be printed to the screen, otherwise "Signature is invalid" will be printed.

Authentication using openssl extension

OpenSSL is a popular encryption and security library that provides many encryption algorithms and security features. OpenSSL extensions have been built into PHP, making it more convenient to use OpenSSL in PHP.

The OpenSSL extension provides another function called openssl_sign() that can sign data. Likewise, the extension also provides the function openssl_verify() for verifying signatures.

The following is an example of signing and verifying using openssl_sign() and openssl_verify() functions:

<?php
$data = "hello world";
$key = openssl_pkey_new(['digest_alg' => 'sha512']);

openssl_sign($data, $signature, $key);

if (openssl_verify($data, $signature, $key)) {
   echo "签名有效";
} else {
   echo "签名无效";
}

In this example, we first use the openssl_pkey_new() function to generate a pair of public and private keys key. The string is then signed using the openssl_sign() function and the signature result is stored in the $signature variable. Finally, use the openssl_verify() function to verify that the signature is valid.

If the signature is valid, then "Signature is valid" will be printed to the screen, otherwise "Signature is invalid" will be printed.

in conclusion

Verification and signature are important methods to ensure data security. In PHP 8, the sodium and openssl extensions will make verification and signing easier. Developers can take advantage of these new features to provide more secure services to companies and customers. If you are a PHP developer, we recommend you take a deep dive into the sodium and openssl extensions to maximize security and protect your users.

The above is the detailed content of New features in PHP 8: Added verification and signing. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
golang中如何验证输入是否为大写字母golang中如何验证输入是否为大写字母Jun 24, 2023 am 09:06 AM

Golang是一门高性能、现代化的编程语言,在日常开发中经常涉及到字符串的处理。其中,验证输入是否为大写字母是一个常见的需求。本文将介绍在Golang中如何验证输入是否为大写字母。方法一:使用unicode包Golang中的unicode包提供了一系列函数来判断字符的编码类型。对于大写字母,其对应的编码范围为65-90(十进制),因此我们可以使用unicod

golang中如何验证输入是否为全角字符golang中如何验证输入是否为全角字符Jun 25, 2023 pm 02:03 PM

在golang中,验证输入是否为全角字符需要用到Unicode编码和rune类型。Unicode编码是一种将字符集中的每个字符分配一个唯一的数字码位的字符编码标准,其中包含了全角字符和半角字符。而rune类型是golang中用于表示Unicode字符的类型。第一步,需要将输入转换为rune类型的切片。这可以通过使用golang的[]rune类型进行转换,例如

golang中如何验证输入是否全部为中文字符golang中如何验证输入是否全部为中文字符Jun 24, 2023 am 09:16 AM

随着时代的发展,我们越来越注重对数据的校验,特别是对用户输入的校验。对于语言类的校验,如何准确判定输入是否全部为中文字符成为了一个重要问题。而在golang中,我们可以借助unicode包和regexp包来实现这一需求。一、unicode包unicode包提供了一系列对于unicode的核心支持。我们可以使用这个包中的函数来准确地判断一个字符是否为中文字符。

PHP正则表达式验证特定字符串开头结尾的方法PHP正则表达式验证特定字符串开头结尾的方法Jun 24, 2023 am 11:20 AM

PHP是一种非常流行的编程语言,常用于Web开发。在PHP开发中,我们经常会遇到需要验证字符串的情况。其中,正则表达式是一种非常常用的方法。在对字符串进行验证时,我们经常需要验证字符串是否以特定字符或字符串开头或结尾。本文将介绍如何使用PHP正则表达式来验证字符串的开头或结尾。验证字符串开头在PHP中,通过正则表达式验证字符串开头,我们可以使用"^"符号来表

在PHP中使用Google reCAPTCHA进行验证在PHP中使用Google reCAPTCHA进行验证Jun 19, 2023 pm 05:38 PM

在现代网络世界中,网站的安全性以及用户隐私的保护越来越成为重要话题。其中,人机验证这一技术方法已经成为防范恶意攻击行为的不可或缺的方式之一。GooglereCAPTCHA,是一个被广泛应用于人机验证的工具,其概念已经深入人心,甚至在我们每天使用的许多网站上都能够看到其存在的身影。在本文中,我们将探讨如何在PHP中使用GooglereCAPTCHA进行验证

golang中如何验证输入是否为英文字母golang中如何验证输入是否为英文字母Jun 24, 2023 am 08:36 AM

作为一门语言,Golang提供了许多方法来方便我们进行数据的验证和处理。其中,验证输入是否为英文字母是一项基本的功能,本篇文章将介绍Golang中两种实现这一功能的方式。1.使用正则表达式正则表达式是一种可以匹配文本片段的表达式。在Golang中,我们可以使用标准库中的regexp包来处理和匹配正则表达式。下面是一个验证输入是否为英文字母的代码示

手机号码验证登录注册的PHP实现指南手机号码验证登录注册的PHP实现指南Aug 17, 2023 pm 03:18 PM

手机号码验证登录注册的PHP实现指南一、概述手机号码验证是现代互联网应用中常见的功能之一,它不仅可以用于用户注册和登录验证,还可以用于短信验证码发送等场景。本文将介绍如何使用PHP语言实现手机号码验证登录注册功能。二、环境要求在开始编写代码之前,我们需要确保以下环境已经准备就绪:PHP环境:PHP的版本需达到5.6或以上。数据库:本文使用MySQL数据库作为

golang中如何验证输入是否为有效的Html标签golang中如何验证输入是否为有效的Html标签Jun 24, 2023 am 08:11 AM

Go语言是一种快速、高效和强类型的编程语言,被广泛应用于网络服务开发、云计算、数据科学、互联网金融等领域。在Web应用开发中,输入验证是一个非常重要的问题,其中验证输入中的HTML标签是否有效是一个常见的需求。下面我们将介绍如何在Go语言中实现这一需求。HTML标签在Web页面中扮演着重要角色,它们定义了页面的结构、样式和交互行为。但在处理用户输入时,我们需

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools