-
Notifications
You must be signed in to change notification settings - Fork 972
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(iOS, Android): add AppUUID Lib for plugins (#5690)
* feat(iOS, Android): add AppUUID Lib for plugins * chore: fmt * chore: swift standard style changes
- Loading branch information
Showing
3 changed files
with
122 additions
and
0 deletions.
There are no files selected for viewing
65 changes: 65 additions & 0 deletions
65
android/capacitor/src/main/java/com/getcapacitor/AppUUID.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package com.getcapacitor; | ||
|
||
import android.content.Context; | ||
import android.content.SharedPreferences; | ||
import androidx.appcompat.app.AppCompatActivity; | ||
import java.nio.charset.StandardCharsets; | ||
import java.security.MessageDigest; | ||
import java.security.NoSuchAlgorithmException; | ||
import java.util.Objects; | ||
import java.util.UUID; | ||
|
||
public final class AppUUID { | ||
|
||
private static final String KEY = "CapacitorAppUUID"; | ||
|
||
public static String getAppUUID(AppCompatActivity activity) throws Exception { | ||
assertAppUUID(activity); | ||
return readUUID(activity); | ||
} | ||
|
||
public static void regenerateAppUUID(AppCompatActivity activity) throws Exception { | ||
try { | ||
String uuid = generateUUID(); | ||
writeUUID(activity, uuid); | ||
} catch (NoSuchAlgorithmException ex) { | ||
throw new Exception("Capacitor App UUID could not be generated."); | ||
} | ||
} | ||
|
||
private static void assertAppUUID(AppCompatActivity activity) throws Exception { | ||
String uuid = readUUID(activity); | ||
if (uuid.equals("")) { | ||
regenerateAppUUID(activity); | ||
} | ||
} | ||
|
||
private static String generateUUID() throws NoSuchAlgorithmException { | ||
MessageDigest salt = MessageDigest.getInstance("SHA-256"); | ||
salt.update(UUID.randomUUID().toString().getBytes(StandardCharsets.UTF_8)); | ||
return bytesToHex(salt.digest()); | ||
} | ||
|
||
private static String readUUID(AppCompatActivity activity) { | ||
SharedPreferences sharedPref = activity.getPreferences(Context.MODE_PRIVATE); | ||
return sharedPref.getString(KEY, ""); | ||
} | ||
|
||
private static void writeUUID(AppCompatActivity activity, String uuid) { | ||
SharedPreferences sharedPref = activity.getPreferences(Context.MODE_PRIVATE); | ||
SharedPreferences.Editor editor = sharedPref.edit(); | ||
editor.putString(KEY, uuid); | ||
editor.apply(); | ||
} | ||
|
||
private static String bytesToHex(byte[] bytes) { | ||
byte[] HEX_ARRAY = "0123456789ABCDEF".getBytes(StandardCharsets.US_ASCII); | ||
byte[] hexChars = new byte[bytes.length * 2]; | ||
for (int j = 0; j < bytes.length; j++) { | ||
int v = bytes[j] & 0xFF; | ||
hexChars[j * 2] = HEX_ARRAY[v >>> 4]; | ||
hexChars[j * 2 + 1] = HEX_ARRAY[v & 0x0F]; | ||
} | ||
return new String(hexChars, StandardCharsets.UTF_8); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import CommonCrypto | ||
import Foundation | ||
|
||
private func hexString(_ iterator: Array<UInt8>.Iterator) -> String { | ||
return iterator.map { String(format: "%02x", $0) }.joined() | ||
} | ||
|
||
extension Data { | ||
public var sha256: String { | ||
var digest = [UInt8](repeating: 0, count: Int(CC_SHA256_DIGEST_LENGTH)) | ||
self.withUnsafeBytes { bytes in | ||
_ = CC_SHA256(bytes.baseAddress, CC_LONG(self.count), &digest) | ||
} | ||
return hexString(digest.makeIterator()) | ||
} | ||
} | ||
|
||
public class AppUUID { | ||
private static let key: String = "CapacitorAppUUID" | ||
|
||
public static func getAppUUID() -> String { | ||
assertAppUUID() | ||
return readUUID() | ||
} | ||
|
||
public static func regenerateAppUUID() { | ||
let uuid = generateUUID() | ||
writeUUID(uuid) | ||
} | ||
|
||
private static func assertAppUUID() { | ||
let uuid = readUUID() | ||
if uuid == "" { | ||
regenerateAppUUID() | ||
} | ||
} | ||
|
||
private static func generateUUID() -> String { | ||
let uuid: String = UUID.init().uuidString | ||
return uuid.data(using: .utf8)!.sha256 | ||
} | ||
|
||
private static func readUUID() -> String { | ||
let defaults = UserDefaults.standard | ||
return defaults.string(forKey: key) ?? "" | ||
} | ||
|
||
private static func writeUUID(_ uuid: String) { | ||
let defaults = UserDefaults.standard | ||
defaults.set(uuid, forKey: key) | ||
} | ||
|
||
} |