admin管理员组文章数量:1410697
Python has functionality to create hex UUID's like so:
>>> import uuid
>>> uuid.uuid4().hex
'47be94c37e484e13ab04ed3c54a5b681'
- Is it possible to do the same in client javascript, with the same hex formatting?
- Is there a way to "validate" the UUID the client sends back? I.e. prevent or notice if a malicious user sends back "1234abcdsh*t..."
A solution idea is to just generate each of the 32 characters randomly on the frontend, but I don't know if there's anything special about the hex UUID's, and I'm also not sure if there's a good way to validate the client sent back a valid value (and not a series of 32 a's)
Edit: just realized there's nothing special about the "hex" formatting, it's just missing the dashes. Will leave that bullet up in case it confuses anyone else.
Python has functionality to create hex UUID's like so:
>>> import uuid
>>> uuid.uuid4().hex
'47be94c37e484e13ab04ed3c54a5b681'
- Is it possible to do the same in client javascript, with the same hex formatting?
- Is there a way to "validate" the UUID the client sends back? I.e. prevent or notice if a malicious user sends back "1234abcdsh*t..."
A solution idea is to just generate each of the 32 characters randomly on the frontend, but I don't know if there's anything special about the hex UUID's, and I'm also not sure if there's a good way to validate the client sent back a valid value (and not a series of 32 a's)
Edit: just realized there's nothing special about the "hex" formatting, it's just missing the dashes. Will leave that bullet up in case it confuses anyone else.
Share Improve this question edited Apr 5, 2018 at 3:57 raphaelrk asked Apr 5, 2018 at 3:33 raphaelrkraphaelrk 8371 gold badge11 silver badges18 bronze badges 9- Is there a way to validate the client sends back a valid UUID? && I'm also not sure if there's a good way to validate the client sent back a valid value - can you clarify what it is you want then? (besides can you do this in JavaScript) – Randy Casburn Commented Apr 5, 2018 at 3:35
- Well that wasn't so hard...I like Google, I remend it really: github./kelektiv/node-uuid – Randy Casburn Commented Apr 5, 2018 at 3:37
- 1 Possible duplicate of Create GUID / UUID in JavaScript? – Randy Casburn Commented Apr 5, 2018 at 3:38
-
2
while that duplicate has some interesting answers, for a v4 UUID nothing beats
const uuidv4 = () => ([1e7]+-1e3+-4e3+-8e3+-1e11).replace(/[018]/g, c => (c ^ crypto.getRandomValues(new Uint8Array(1))[0] & 15 >> c / 4).toString(16));
... orconst uuidv4hex = () => ([1e7,1e3,4e3,8e3,1e11].join('')).replace(/[018]/g, c => (c ^ crypto.getRandomValues(new Uint8Array(1))[0] & 15 >> c / 4).toString(16));
for the.hex
equivalent :p – Jaromanda X Commented Apr 5, 2018 at 3:48 -
1
well, validating would require code on the server to check - you haven't specified your server language, so, all I can say is that you need to make sure the 13th? character is 4, and and the 17th character is
8->f
(I think) – Jaromanda X Commented Apr 5, 2018 at 3:55
1 Answer
Reset to default 3You can use buffer to convert to hex conveniently.
const uuid = require('uuid')
const buffer = Buffer.alloc(16);
uuid.v4({}, buffer);
console.log(buffer.toString('hex'));
Not the cleanest and most elegant solution but will get the job done.
本文标签: Equivalent to python39s uuiduuid4()hex in javascriptStack Overflow
版权声明:本文标题:Equivalent to python's uuid.uuid4().hex in javascript? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744902589a2631430.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论