I'm using the following typescript method to generate UUID
s. The code itself is basically the typescript version of this stackoverflow answer.
generateUUID(): string {
let date = new Date().getTime();
if (window.performance && typeof window.performance.now === 'function') {
date += performance.now();
}
let uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
let r = (date + Math.random() * 16) % 16 | 0;
date = Math.floor(date / 16);
return (c === 'x' ? r : (r & 0x3 | 0x8)).toString(16);
});
return uuid;
};
Our development team uses TSLint
to keep the code clean and we have a rule that forbids to use bitwise operators
. I have no idea how to rewrite this code without harming the cryptographic aspect of the UUID generator. How can this piece of code be rewritten or doesn't this make sense at all?
The reason TSLint highlights this is because it is more likely the bitwise operator has been used accidentally (for instance, in an if statement) than to have used it on purpose.
It should be quite acceptable to tell TSLint that you really meant to use a bitwise operator. Just wrap them in special TSLint comments. :