Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sha256 base64 #9540

Open
lscool66 opened this issue Jun 27, 2023 · 2 comments
Open

sha256 base64 #9540

lscool66 opened this issue Jun 27, 2023 · 2 comments
Labels
question the issue author asks something

Comments

@lscool66
Copy link

lscool66 commented Jun 27, 2023

Question

import hashlib
import base64
import hmac

def gen_sign(timestamp, secret):
    # 拼接timestamp和secret
    string_to_sign = '{}\n{}'.format(timestamp, secret)
    print(string_to_sign)
    hmac_code = hmac.new(string_to_sign.encode("utf-8"), digestmod=hashlib.sha256).digest()

    print(hmac_code)

    # 对结果进行base64处理
    sign = base64.b64encode(hmac_code).decode('utf-8')

    return sign
let secret = "123"
let timestamp = (date now | into int | into string | str substring 0..10)
let sign = ($timestamp + "\n" + $secret | hash sha256 | encode base64)

python 和 nu 的结果不一致请问应该如何写nu

Additional context and details

No response

@lscool66 lscool66 added the question the issue author asks something label Jun 27, 2023
@fdncred
Copy link
Collaborator

fdncred commented Jun 27, 2023

I'm not sure exactly but it looks like it may have something to do with how hmac and base64 are being used. hmac is producing binary data. To do something similar, you'd need to do hash sha256 --binary. You also need to understand how base64.b64encode is being used so you can use the same parameters in nushell. We allow character sets. You can see this with encode base64 --help.

@lscool66
Copy link
Author

lscool66 commented Jun 29, 2023

func GenSign(secret string, timestamp int64) (string, error) {
 //timestamp + key 做sha256, 再进行base64 encode
 stringToSign := fmt.Sprintf("%v", timestamp) + "\n" + secret

 var data []byte
 h := hmac.New(sha256.New, []byte(stringToSign))
 _, err := h.Write(data)
 if err != nil {
    return "", err
 }

 signature := base64.StdEncoding.EncodeToString(h.Sum(nil))
 return signature, nil
}

我这边使用go的交叉编译来解决这个问题

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question the issue author asks something
Projects
None yet
Development

No branches or pull requests

2 participants