Explain the system - PHP Interface signature verification

Overview of

work, we deal with at all times, and interfaces, some others retrieved interface provides an interface to some others in this process certainly can not do without signature verification.

When designing signature verification, must meet the following:

    variability: Each signature must be different.

    Timeliness: timeliness every request, expired.

    Uniqueness: Each signature is unique.

    Integrity: the ability to validate incoming data against tampering.

The following major share some of the work of encryption and decryption methods commonly used.
Common validation

Example:? / Api / login username = xxx & password = xxx & sign = xxx

sender and receiver agree on a value encrypted salt, for generating a signature.

Code Example:



Using the above method to MD5, MD5 one-way hash encryption belongs.
A one-way cryptographic hashing
define

the change in the input string of arbitrary length to a fixed length output string, and it is difficult to obtain the input string from the output string, this method is called single hashed.
Commonly used algorithm

    MD5

    the SHA

    the MAC

    the CRC

advantages

in Example MD5.

    Convenience store: is a fixed size (32) of the encrypted string, storage space can be allocated a fixed size.

    Low loss: encryption / encryption for minimal loss of properties.

    File Encryption: 32 only need to be able to string a huge file to verify its integrity.

    Irreversible: irreversible in most cases, with good security.

Shortcoming

    possibility of brute force, the best way to improve security by salt value.

Application scenario

    for sensitive data, such as user password, request parameters, the file encryption.

Recommended password storage

password_hash () with sufficient strength of a one-way hash algorithm to create a password hash (hash).

Sample code:



the PHP Manual Address:

http://php.net/manual/zh/function.password-hash.php
symmetric encryption
define

the same key may be used to encrypt and decrypt data, this method is called symmetric encryption .
Commonly used algorithms

    DES

    AES

AES is an upgraded version of DES, longer key lengths, more options, more flexible, more secure and faster.
Advantage of

the algorithm disclosed, a small amount of calculation, high speed encryption, the encryption efficiency high.
Disadvantage of

the sender and receiver must agree on the key is good, then both sides can keep keys, key management becomes the burden of both sides.
Scenarios

relatively large amount of data or encryption key data point.
AES

AES encryption libraries can easily find online, please pay attention to the class library mcrypt_encrypt and mcrypt_decrypt method!




In PHP7.2 version it has been abandoned, and openssl_decrypt openssl_encrypt use two methods in the new version.

Sample Code (library):



Sample Code:



Run Results:


Asymmetric encryption
definitions

requires two keys for encryption and decryption, the two keys are the public key (public key) and a private key (private key), which method is called asymmetric encryption.
Commonly used algorithm

    RSA

advantages

compared with symmetric encryption, better security, require different decryption keys, public and private keys are available for each of encryption and decryption.
Disadvantage of

encryption and decryption takes a long time, slow, suitable only for small amounts of data to be encrypted.
Scenarios

suitable for demanding security scenarios for encrypting small amounts of data, such as payment data, log data.
RSA and RSA2
algorithm name Standard Name Remarks
RSA2 SHA256WithRSA mandatory RSA key length of at least 2048
RSA SHA1withRSA no limitation on the length of RSA keys, we recommend 2048 or more

RSA2 stronger than RSA security capabilities.

Ants gold dress, Sina Weibo are using RSA2 algorithm.

Create public and private key:

    openssl genrsa -out private_key.pem 2048    
    openssl rsa -in private_key.pem -pubout -out public_key.pem

 



执行上面命令,会生成 private_key.pem 和 public_key.pem 两个文件。

示例代码(类库):


示例代码:



运行结果:

部分数据截图如下:


JS-RSA

JSEncrypt :用于执行OpenSSL RSA加密、解密和密钥生成的Javascript库。

Git源:https://github.com/travist/jsencrypt

应用场景:

我们在做 WEB 的登录功能时一般是通过 Form 提交或 Ajax 方式提交到服务器进行验证的。

为了防止抓包,登录密码肯定要先进行一次加密(RSA),再提交到服务器进行验证。

一些大公司都在使用,比如淘宝、京东、新浪 等。

示例代码就不提供了,Git上提供的代码是非常完善的。
密钥安全管理

这些加密技术,能够达到安全加密效果的前提是 密钥的保密性。

实际工作中,不同环境的密钥都应该不同(开发环境、预发布环境、正式环境)。

那么,应该如何安全保存密钥呢?
环境变量

将密钥设置到环境变量中,每次从环境变量中加载。
配置中心

将密钥存放到配置中心,统一进行管理。
密钥过期策略

设置密钥有效期,比如一个月进行重置一次。

在这里希望大佬提供新的思路 ~
接口调试工具
Postman

一款功能强大的网页调试与发送网页 HTTP 请求的 Chrome插件。

这个不用多介绍,大家肯定都使用过。
SocketLog

Git源:https://github.com/luofei614/SocketLog

解决的痛点:


    正在运行的API有Bug,不能在文件中使用var_dump进行调试,因为会影响到client的调用。将日志写到文件中,查看也不是很方便。



    我们在二次开发一个新系统的时候,想查看执行了哪些Sql语句及程序的warning,notice等错误信息。


SocketLog,可以解决以上问题,它通过WebSocket将调试日志输出到浏览器的console中。

使用方法

    安装、配置Chrome插件

    SocketLog服务端安装

    PHP中用SocketLog调试

    配置日志类型和相关参数

在线接口文档

接口开发完毕,需要给请求方提供接口文档,文档的编写现在大部分都使用Markdown格式。

也有一些开源的系统,可以下载并安装到自己的服务器上。

也有一些在线的系统,可以在线使用同时也支持离线导出。

根据自己的情况,选择适合自己的文档平台吧。

常用的接口文档平台:

    eolinker

    Apizza

    Yapi

    RAP2

    DOClever

扩展

一、在 HTTP 和 RPC 的选择上,可能会有一些疑问,RPC框架配置比较复杂,明明用HTTP能实现为什么要选择RPC?

下面简单的介绍下 HTTP 与 RPC 的区别。

传输协议:

    HTTP 基于 HTTP 协议。

    RPC 即可以 HTTP 协议,也可以 TCP 协议。

HTTP 也是 RPC 实现的一种方式。

性能消耗:

    HTTP 大部分基于 JSON 实现的,序列化需要时间和性能。

    RPC 可以基于二进制进行传输,消耗性能少一点。

推荐一个像 JSON ,但比 JSON 传输更快占用更少的新型序列化类库 MessagePack。

官网地址:https://msgpack.org/

还有一些服务治理、负载均衡配置的区别。

使用场景:

比如浏览器接口、APP接口、第三方接口,推荐使用 HTTP。

比如集团内部的服务调用,推荐使用 RPC。

RPC 比 HTTP 性能消耗低,传输效率高,服务治理也方便。

推荐使用的 RPC 框架:Thrift。

二、动态令牌

简单介绍下几种动态令牌,感兴趣的可以深入了解下。

OTP:One-Time Password 一次性密码。

HOTP:HMAC-based One-Time Password 基于HMAC算法加密的一次性密码。

TOTP:Time-based One-Time Password 基于时间戳算法的一次性密码。

使用场景:

    公司VPN登录双因素验证

    服务器登录动态密码验证

    网银、网络游戏的实体动态口令牌

    银行转账动态密码

    ...

小结

本文讲了设计签名验证需要满足的一些条件:可变性、时效性、唯一性、完整性。

还讲了一些加密方法:单向散列加密、对称加密、非对称加密,同时分析了各种加密方法的优缺点,大家可以根据自己的业务特点进行自由选择。

提供了 Aes、Rsa 相关代码示例。

分享了可以编写接口文档的在线系统。

分享了开发过程中使用的接口调试工具。

扩展中分析了 HTTP 和 RPC 的区别,动态令牌的介绍等。

Guess you like

Origin www.cnblogs.com/it-3327/p/11820330.html