0

Verify UUID from javascript similar to Hibernate. I know UUID is unique and no need to worry but out of curiosity I want to dig down more and want my application to generate similar UUID from javascript and hibernate.

In Hibernate

@Id
@GeneratedValue(generator = "uuid2")
@GenericGenerator(name = "uuid2", strategy = "uuid2")
@Column(name = "id", columnDefinition = "BINARY(16)")
private UUID id;

Generated by Hibernate

"6613d9b1-dcc8-47bf-8d85-bc4415385c42"
"2f160bae-f53a-472b-93eb-5ecc352fbac7"

In Javascript (https://stackoverflow.com/a/2117523/2381453)

function uuidv4() {
  return ([1e7]+-1e3+-4e3+-8e3+-1e11).replace(/[018]/g, c =>
    (c ^ crypto.getRandomValues(new Uint8Array(1))[0] & 15 >> c / 4).toString(16)
  )
}

console.log(uuidv4());

Generated by JavaScript

"37ad0abe-fbf0-4a91-aa59-e02e81ff1a96"
"c5969fe4-dcbf-4af6-93f5-4a7011dd6f24"

So is it the same?

Vicky Thakor
  • 3,847
  • 7
  • 42
  • 67
  • That UUID function is completely bonkers. – tadman Nov 18 '17 at 05:17
  • Then, can you point me to correct version? I just copied from one of the stackoverflow answer (https://stackoverflow.com/a/2117523/2381453) – Vicky Thakor Nov 18 '17 at 05:19
  • It's a valid function, but what I mean is that is some really intense JavaScript code. It's "adding" together numbers to create a template string, then going to town with a cryptographic random number generator. Clever, but really not the most self-explanatory. – tadman Nov 18 '17 at 05:27
  • `So is it the same?` ... looks like it's the "same", why do you doubt it? – Jaromanda X Nov 18 '17 at 06:01

0 Answers0