Skip to content

Commit 470f8b5

Browse files
authored
feat: 增加 rsaCheck 验签方法 (#142)
closes #140
1 parent d2a11da commit 470f8b5

File tree

3 files changed

+44
-7
lines changed

3 files changed

+44
-7
lines changed

README.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,34 @@ const success = sdk.checkNotifySignV2(postData);
362362
const plainText = alipaySdk.aesDecrypt(getPhoneNumberResponse);
363363
```
364364

365+
### 对前端返回的报文进行验签
366+
367+
参考 https://opendocs.alipay.com/common/02mse3#AES%20%E8%A7%A3%E5%AF%86%E5%87%BD%E6%95%B0 的算法
368+
369+
前端返回的内容
370+
371+
```json
372+
{
373+
"response": "hvDOnibG0DPcOFPNubK3DEfLQGL4=",
374+
"sign": "OIwk7zfZMp5GX78Ow==",
375+
"sign_type": "RSA2",
376+
"encrypt_type": "AES",
377+
"charset": "UTF-8"
378+
}
379+
```
380+
381+
通过 alipay-sdk 验签
382+
383+
```ts
384+
// 注意,加密内容必须前后加上双引号
385+
const signContent = '"hvDOnibG0DPcOFPNubK3DEfLQGL4="';
386+
const sign = 'OIwk7zfZMp5GX78Ow==';
387+
const signType = 'RSA2';
388+
const signCheckPass = alipaySdk.rsaCheck(signContent, sign, signType);
389+
390+
console.log(signCheckPass);
391+
```
392+
365393
## alipay-sdk v3 到 v4 的升级说明
366394

367395
从 v3 到 v4 有以下不兼容变更,请参考示例代码进行更新

src/alipay.ts

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import camelcaseKeys from 'camelcase-keys';
99
import snakeCaseKeys from 'snakecase-keys';
1010
import { Stream as SSEStream } from 'sse-decoder';
1111
import { AlipayFormStream } from './AlipayFormStream.js';
12-
import type { AlipaySdkConfig } from './types.js';
12+
import type { AlipaySdkConfig, AlipaySdkSignType } from './types.js';
1313
import { AlipayFormData } from './form.js';
1414
import {
1515
sign, ALIPAY_ALGORITHM_MAPPING, decamelize, createRequestId, readableToBytes,
@@ -732,7 +732,7 @@ export class AlipaySdk {
732732
}
733733

734734
// 消息验签
735-
private notifyRSACheck(signArgs: { [key: string]: any }, signStr: string, signType: 'RSA' | 'RSA2', raw?: boolean) {
735+
private notifyRSACheck(signArgs: { [key: string]: any }, signStr: string, signType: AlipaySdkSignType, raw?: boolean) {
736736
const signContent = Object.keys(signArgs).sort().filter(val => val)
737737
.map(key => {
738738
let value = signArgs[key];
@@ -748,10 +748,7 @@ export class AlipaySdk {
748748
return `${key}=${decodeURIComponent(value)}`;
749749
})
750750
.join('&');
751-
752-
const verifier = createVerify(ALIPAY_ALGORITHM_MAPPING[signType]);
753-
754-
return verifier.update(signContent, 'utf8').verify(this.config.alipayPublicKey, signStr, 'base64');
751+
return this.rsaCheck(signContent, signStr, signType);
755752
}
756753

757754
/**
@@ -1024,4 +1021,14 @@ export class AlipaySdk {
10241021
aesDecrypt(encryptedText: string) {
10251022
return aesDecryptText(encryptedText, this.config.encryptKey);
10261023
}
1024+
1025+
/**
1026+
* 对指定内容进行验签
1027+
*
1028+
* 如对前端返回的报文进行验签 https://opendocs.alipay.com/common/02mse3#AES%20%E8%A7%A3%E5%AF%86%E5%87%BD%E6%95%B0
1029+
*/
1030+
rsaCheck(signContent: string, sign: string, signType: AlipaySdkSignType = 'RSA2') {
1031+
const verifier = createVerify(ALIPAY_ALGORITHM_MAPPING[signType]);
1032+
return verifier.update(signContent, 'utf-8').verify(this.config.alipayPublicKey, sign, 'base64');
1033+
}
10271034
}

src/types.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
export type AlipaySdkSignType = 'RSA2' | 'RSA';
2+
13
/**
24
* @interface AlipaySdkConfig SDK 配置
35
*/
@@ -7,7 +9,7 @@ export interface AlipaySdkConfig {
79
/** 应用私钥字符串。RSA签名验签工具:https://docs.open.alipay.com/291/106097)*/
810
privateKey: string;
911
/** 签名种类,默认是 RSA2 */
10-
signType?: 'RSA2' | 'RSA';
12+
signType?: AlipaySdkSignType;
1113
/** 支付宝公钥(需要对返回值做验签时候必填) */
1214
alipayPublicKey?: string;
1315
/** 网关 */

0 commit comments

Comments
 (0)