Page Menu
Home
Phabricator
Search
Configure Global Search
Log In
Files
F3353046
D4587.id14959.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
1014 B
Referenced Files
None
Subscribers
None
D4587.id14959.diff
View Options
diff --git a/web/utils/device-id.js b/web/utils/device-id.js
new file mode 100644
--- /dev/null
+++ b/web/utils/device-id.js
@@ -0,0 +1,34 @@
+// @flow
+import crypto from 'crypto';
+
+export const deviceIDTypes = Object.freeze({
+ KEYSERVER: 0,
+ WEB: 1,
+ MOBILE: 2,
+});
+export type DeviceIDType = $Values<typeof deviceIDTypes>;
+
+const alphanumeric =
+ '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
+const deviceIDLength = 64;
+
+function generateDeviceID(type: DeviceIDType): string {
+ const rand = crypto.randomBytes(deviceIDLength);
+ let suffix = '';
+
+ for (let i = 0; i < deviceIDLength; i++) {
+ const index = rand[i] % alphanumeric.length;
+ suffix += alphanumeric.charAt(index);
+ }
+
+ if (type === deviceIDTypes.KEYSERVER) {
+ return `ks:${suffix}`;
+ } else if (type === deviceIDTypes.WEB) {
+ return `web:${suffix}`;
+ } else if (type === deviceIDTypes.MOBILE) {
+ return `mobile:${suffix}`;
+ }
+ return '';
+}
+
+export { generateDeviceID };
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Sun, Nov 24, 7:50 AM (18 h, 52 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2574622
Default Alt Text
D4587.id14959.diff (1014 B)
Attached To
Mode
D4587: [web] Add function (and an 'enum') for creating unique device ID on web
Attached
Detach File
Event Timeline
Log In to Comment