about summary refs log tree commit diff
path: root/android/app/src/main/java/xyz/blueskyweb/pubsq/AppSecureRandomModule.java
blob: 3724d9603dbc13ad3611f8deddba2cdc8240b339 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
package app.bsky;

import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.Promise;

import java.security.SecureRandom;
import android.util.Base64;

public class AppSecureRandomModule extends ReactContextBaseJavaModule {
  public AppSecureRandomModule(ReactApplicationContext context) {
    super(context);
  }

  @ReactMethod
  public void generateSecureRandomAsBase64(int length, Promise promise) {
    SecureRandom secureRandom = new SecureRandom();
    byte[] buffer = new byte[length];
    secureRandom.nextBytes(buffer);
    promise.resolve(Base64.encodeToString(buffer, Base64.NO_WRAP));
  }

  @Override
  public String getName() {
    return "AppSecureRandomModule";
  }
}