generate “Sec-WebSocket-Accept” from “Sec-WebSocket-Key”

后端 未结 1 1203
予麋鹿
予麋鹿 2020-12-10 19:11

I\'m following rfc6455:

Concretely, if as in the example above, the |Sec-WebSocket-Key|
header field had the value \"dGhlIHNhbXBsZSBub25jZQ==\",

相关标签:
1条回答
  • 2020-12-10 19:40

    You need to base64-encode the raw sha1 digest.
    You are encoding the hexadecimal string representation of the digest which is double the length.

    Online tools work with text and don't work with raw binary data, that's why you are getting wrong results.

    Python example:

    import hashlib, base64
    h = hashlib.sha1("dGhlIHNhbXBsZSBub25jZQ==258EAFA5-E914-47DA-95CA-C5AB0DC85B11")
    print "hexdigest:", h.hexdigest() # hexadecimal string representation of the digest
    print "digest:", h.digest() # raw binary digest
    print
    print "wrong result:", base64.b64encode(h.hexdigest())
    print "right result:", base64.b64encode(h.digest())
    

    This prints:

    hexdigest: b37a4f2cc0624f1690f64606cf385945b2bec4ea
    digest: ᄈzO,ÀbOミöFÏ8YEᄇᄒÄê
    
    wrong result: YjM3YTRmMmNjMDYyNGYxNjkwZjY0NjA2Y2YzODU5NDViMmJlYzRlYQ==
    right result: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=
    
    0 讨论(0)
提交回复
热议问题