Skip to content

Commit

Permalink
fix: use crypto-js in case of http domain
Browse files Browse the repository at this point in the history
  • Loading branch information
noe132 committed Nov 11, 2022
1 parent 0c38435 commit 09922f3
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 15 deletions.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@
"license": "MIT",
"dependencies": {
"@babel/runtime-corejs3": "^7.17.8",
"@types/crypto-js": "^4.1.1",
"base64-arraybuffer": "^1.0.2",
"crypto-js": "^4.1.1",
"eslint": "^8.12.0",
"fp-ts": "^2.11.9",
"io-ts": "^2.2.16",
Expand Down
21 changes: 6 additions & 15 deletions src/provider/AliApiTranslate/translate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { isLeft, left, right } from 'fp-ts/Either'
import { encode } from 'base64-arraybuffer'
import md5 from 'md5'
import { v4 as randomUUID } from 'uuid'
import hmacSha1 from 'crypto-js/hmac-sha1'

import { got } from '~/util/gmapi'
import containerData from '~/provider/AliApiTranslate/container/data'
Expand All @@ -24,23 +25,13 @@ const MD5Base64 = (str: string) => {
return md5Base64
}

const HMACSha1 = async (str: string, key: string) => {
const HMACSha1 = (str: string, key: string) => {
if (str === '') {
return ''
}
const keyUint8Arr = new TextEncoder().encode(key)
const singingKey = await window.crypto.subtle.importKey(
'raw',
keyUint8Arr,
{ name: 'HMAC', hash: 'SHA-1' },
false,
['sign'],
)
const dataUint8Arr = new TextEncoder().encode(str)
const signUint8Arr = await window.crypto.subtle.sign('HMAC', singingKey, dataUint8Arr)
const sign2 = encode(signUint8Arr)

return sign2
const sign = encode(hexToUint8Array(hmacSha1(str, key).toString()))
sign.toString()
return sign
}

function trimIndent(str: string) {
Expand Down Expand Up @@ -69,7 +60,7 @@ const doRequest = async (body: string, appId: string, appKey: string) => {
x-acs-signature-nonce:${uuid}
x-acs-version:${version}
${apiPath}`
const signature = await HMACSha1(trimIndent(stringToSign), appKey)
const signature = HMACSha1(trimIndent(stringToSign), appKey)
const authorization = `acs ${appId}:${signature}`

const sendPost = async () => {
Expand Down
10 changes: 10 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1450,6 +1450,11 @@
dependencies:
"@types/node" "*"

"@types/crypto-js@^4.1.1":
version "4.1.1"
resolved "https://registry.npmmirror.com/@types/crypto-js/-/crypto-js-4.1.1.tgz#602859584cecc91894eb23a4892f38cfa927890d"
integrity sha512-BG7fQKZ689HIoc5h+6D2Dgq1fABRa0RbBWKBd9SP/MVRVXROflpm5fhwyATX5duFmbStzyzyycPB8qUYKDH3NA==

"@types/eslint-scope@^3.7.3":
version "3.7.3"
resolved "https://registry.npmmirror.com/@types/eslint-scope/-/eslint-scope-3.7.3.tgz#125b88504b61e3c8bc6f870882003253005c3224"
Expand Down Expand Up @@ -2829,6 +2834,11 @@ [email protected]:
resolved "https://registry.npm.taobao.org/crypt/download/crypt-0.0.2.tgz#88d7ff7ec0dfb86f713dc87bbb42d044d3e6c41b"
integrity sha1-iNf/fsDfuG9xPch7u0LQRNPmxBs=

crypto-js@^4.1.1:
version "4.1.1"
resolved "https://registry.npmmirror.com/crypto-js/-/crypto-js-4.1.1.tgz#9e485bcf03521041bd85844786b83fb7619736cf"
integrity sha512-o2JlM7ydqd3Qk9CA0L4NL6mTzU2sdx96a+oOfPu8Mkl/PK51vSyoi8/rQ8NknZtk44vq15lmhAj9CIAGwgeWKw==

css-blank-pseudo@^3.0.3:
version "3.0.3"
resolved "https://registry.npmmirror.com/css-blank-pseudo/-/css-blank-pseudo-3.0.3.tgz#36523b01c12a25d812df343a32c322d2a2324561"
Expand Down

0 comments on commit 09922f3

Please sign in to comment.