Skip to content

Instantly share code, notes, and snippets.

View ohaval's full-sized avatar
💭
Code can be a piece of art

Ohav ohaval

💭
Code can be a piece of art
View GitHub Profile
/*
This program receives the accurate string of the username (as written by `net user`)
and log offs the session associated with that user, if any is active.
In order to log off another users on the system, Administrator permissions
are required. Thus, the program should be run as an Administrator.
Flow of the code is:
1) Enumerate active sessions
2) For each session, enumerate the username string associated with the session
@ohaval
ohaval / logoff_user.py
Created September 8, 2021 12:09
A small program that log off a user (possibly in another session) in Windows
"""A small program that log off a user (possibly in another session) in Windows
This is actually an implementation in Python of the `LogoffUser.cpp` program I have made.
I made that program in order to automate the manual operation of logging off my family
user in the workstation each time after they have used it :)
LogoffUser.cpp link: https://gist.github.com/ohaval/a922fc07f4585b81c119614e852d9dbd
Make sure that the `pywin32` packages is installed before using
@ohaval
ohaval / SaveImageFromClipboard.cpp
Last active April 22, 2023 09:03
Save an image from the clipboard to a file
/*
I wanted to play with the clipboard via Windows API so I wrote a small program that
checks if the clipboard contains an image, and if so saves the image as a .bmp file.
This program obtains the handle on the clipboard data, put together all of the
information and raw data a .bmp file holds, and writes it to the disk.
With small changes it's possible to make this code run in a loop and save an image
from the clipboard every X seconds.
@ohaval
ohaval / quick_sort.py
Created October 1, 2021 12:15
The Quicksort algorithm simple and readable implementation in Python
from typing import List, Tuple
def quick_sort(lst: List[int]) -> List[int]:
"""The Quicksort algorithm.
Wikipedia: https://en.wikipedia.org/wiki/Quicksort
"""
if len(lst) < 2:
return lst
@ohaval
ohaval / bubble_sort.py
Created October 1, 2021 12:20
The Bubble sort algorithm simple and readable implementation in Python
from typing import List
def bubble_sort(lst: List[int]) -> None:
"""The Bubble sort algorithm.
The sorted list is placed in place.
Wikipedia: https://en.wikipedia.org/wiki/Bubble_sort
"""
for i in range(len(lst) - 1, 0, -1):
@ohaval
ohaval / merge_sort.py
Created October 1, 2021 12:23
The Merge sort algorithm simple and readable implementation in Python
from typing import List
def merge_sort(lst: List[int]) -> List[int]:
"""The Merge sort algorithm.
Wikipedia: https://en.wikipedia.org/wiki/Merge_sort
"""
if len(lst) <= 1:
return lst
@ohaval
ohaval / insertion_sort.py
Created October 1, 2021 12:25
The Insertion sort algorithm simple and readable implementation in Python
from typing import List
def insertion_sort(lst: List[int]) -> None:
"""The Insertion sort algorithm.
The sorted list is placed in place.
Wikipedia: https://en.wikipedia.org/wiki/Insertion_sort
"""
for i in range(1, len(lst)):
@ohaval
ohaval / selection_sort.py
Created October 1, 2021 12:29
The Selection sort algorithm simple and readable implementation in Python
from typing import List
def selection_sort(lst: List[int]) -> None:
"""The Selection sort algorithm.
The sorted list is placed in place.
Wikipedia: https://en.wikipedia.org/wiki/Selection_sort
"""
for i in range(len(lst) - 1):
@ohaval
ohaval / cycle_sort.py
Created October 1, 2021 12:32
The Cycle sort algorithm simple and readable implementation in Python
from typing import List
def cycle_sort(lst: List[int]) -> None:
"""The Cycle sort algorithm.
The sorted list is placed in place.
Wikipedia: https://en.wikipedia.org/wiki/Cycle_sort
"""
for i in range(len(lst) - 1):
@ohaval
ohaval / export_ecdsa_keys.py
Created November 1, 2021 17:34
Generate and export ECDSA public and private keys in Python.
"""This example shows how easy it is to generate and export ECDSA keys with python.
This program is similar to `ssh-keygen -t ecdsa` with no passphrase.
To export the private key with a passphrase, read paramiko.pkey.PKey._write_private_key method.
"""
import paramiko
from cryptography.hazmat.primitives.serialization import (
Encoding, PrivateFormat, PublicFormat, NoEncryption
)