Skip to content

Instantly share code, notes, and snippets.

View jackz314's full-sized avatar

Jack Zhang jackz314

View GitHub Profile
@jackz314
jackz314 / closest.py
Last active June 9, 2020 04:22
Get element index closest to target element in a list of tuples
# Modified from solution for numbers here: https://stackoverflow.com/a/12141511/8170714
# Tuple comparison solution from here: https://stackoverflow.com/a/31779268/8170714
def take_closest(self, list, element, idx=0):
"""
Assumes myList is sorted. Returns closest value to element (tuple).
When comparing not all elements in tuple, set all other locations in tuple to nothing (e.g. (X,Y,)).
@jackz314
jackz314 / multi_prime_rsa.py
Created October 2, 2019 06:35
Multi Prime RSA solver
# Solves multi prime rsa given n, e, and c. Need to factor n into primes first (recommend yafu)
# Reference https://crypto.stackexchange.com/questions/31109/rsa-enc-decryption-with-multiple-prime-modulus-using-crt
# From https://github.com/diogoaj/ctf-writeups/tree/master/2018/Timisoara/crypto/NotYourAverageRSA
# Params
e = 65537
c = 48761539940486768790697951968441053167086423529120379009399989923982917278530780108524481919294548305561552133247376067350664771674488982501980538923179804440135482761541868213581098181220801732284669971107195377327445661261746882474615837238429855596647745621191046720648860759474615170945636435027382702345930153884587334870109990234396501579
n = 81736943705459767985288486167314099164146317197040392194768161097750074479540025761100109449092862009195976097250151609584294118669228141027624354052423638509988705830737675936098155468596924772948252465412194715615408850250410310761063399013426728554729053139453019049285162533445627620506060381552244023004446417793032764776342793336374
@jackz314
jackz314 / disable.bat
Created July 12, 2019 18:55
Turn off Hyper-V on Windows completely (for VT-X to work)
[in Windows UI]
Turn on/off Windows Features -> Untick Hyper-V
[in cmd or PowerShell]
bcdedit /set hypervisorlaunchtype off
[in PowerShell]
dism.exe /Online /Disable-Feature:Microsoft-Hyper-V
@jackz314
jackz314 / occur.sh
Created June 29, 2019 22:11
Sort occurrence of words among a directory of files
cat *.txt | tr '[:space:]' '[\n*]' | grep -v "^\s*$" | sort | uniq -c | sort -bnr | head -n 100
#extension .txt, limit result to top 100. Change if needed
@jackz314
jackz314 / DragEditText.java
Created April 20, 2019 02:44
Drag to select text with a mouse inside Android EditText like on computers
EditText mText = ....
final int[] beginOffset = {-1};
View.OnTouchListener otl = new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getToolType(0) == MotionEvent.TOOL_TYPE_MOUSE){//only change behavior for mouse
int i = event.getAction();
if (i == MotionEvent.ACTION_DOWN) {
Layout layout = ((EditText) v).getLayout();
float x = event.getX() + mText.getScrollX();
@jackz314
jackz314 / SetEditTextCursorColor.java
Last active June 9, 2020 04:14
A reflection method used to set EditText Cursor Color with support for Android P (API 28)
//StackOverflow solution by me here: https://stackoverflow.com/a/52564925/8170714
public static void setEditTextCursorColor(EditText editText, int color) {
try {
// Get the cursor resource id
Field field = TextView.class.getDeclaredField("mCursorDrawableRes");
field.setAccessible(true);
int drawableResId = field.getInt(editText);
// Get the editor
@jackz314
jackz314 / openPreference.java
Last active September 29, 2018 03:49
Open a Preference in Android Setting (preference) Activity
private void openPreference(String key) {
PreferenceScreen preferenceScreen = getPreferenceScreen();
final ListAdapter listAdapter = preferenceScreen.getRootAdapter();
final int itemsCount = listAdapter.getCount();
int itemNumber;
for (itemNumber = 0; itemNumber < itemsCount; ++itemNumber) {
if (listAdapter.getItem(itemNumber).equals(findPreference(key))) {
preferenceScreen.onItemClick(null, null, itemNumber, 0);
break;
@jackz314
jackz314 / DetectPastTime.java
Last active September 29, 2018 03:48
Detect Past Times In a String
String[] pastTimeIndicators = new String[]{"last sec", "last min", "last hr", "last hour", "yesterday", "last week", "last month", "last year"};
if(isStringContainAnyOfTheseWords(group.getText().toLowerCase(), pastTimeIndicators)){
Toast.makeText(getContext(),getString(R.string.past_time_warning),Toast.LENGTH_LONG).show();
Calendar opCalendar = Calendar.getInstance();
opCalendar.setTime(currentTime);
opCalendar.add(Calendar.SECOND,-1);
long lastSecTime = opCalendar.getTimeInMillis();
opCalendar.setTime(currentTime);
opCalendar.add(Calendar.MINUTE,-1);
long lastMinTime = opCalendar.getTimeI