Skip to content

Instantly share code, notes, and snippets.

Every so often I have to restore my gpg keys and I'm never sure how best to do it. So, I've spent some time playing around with the various ways to export/import (backup/restore) keys.

Method 1

Backup the public and secret keyrings and trust database

cp ~/.gnupg/pubring.gpg /path/to/backups/
cp ~/.gnupg/secring.gpg /path/to/backups/
cp ~/.gnupg/trustdb.gpg /path/to/backups/

or, instead of backing up trustdb...

@m00nlight
m00nlight / gist:e4fc71e0d37b51842e5f
Last active August 29, 2015 14:14
Python N-Queen problem
class NQueen:
# @return a list of lists of string
def solveNQueens(self, n):
self.__ans = []
self.__stack = []
self.__n = n
self.solve(0)
return self.__ans
@m00nlight
m00nlight / gist:daa6786cc503fde12a77
Last active March 7, 2024 09:23
Python KMP algorithm
class KMP:
def partial(self, pattern):
""" Calculate partial match table: String -> [Int]"""
ret = [0]
for i in range(1, len(pattern)):
j = ret[i - 1]
while j > 0 and pattern[j] != pattern[i]:
j = ret[j - 1]
ret.append(j + 1 if pattern[j] == pattern[i] else j)
@m00nlight
m00nlight / gist:245d917cb030c515c513
Last active June 25, 2022 05:57
Python heap optimize dijkstra algorithm
import sys
from heapq import heappush, heappop
class Dijkstra:
def __init__(self, adjacents):
self.adj = adjacents
self.n = len(adjacents)
def dijkstra(self, start):
dis, vis, hq = {}, {}, []
@m00nlight
m00nlight / gist:7f3d477e541e47d53ec1
Created February 3, 2015 02:09
Python compact radix sort of numbers
class RadixSort:
def radix_sort(self, nums):
"""
Input an number list and return an sortd list
Type: [Int] -> [Int]
"""
max_len = max(map(lambda x: len(str(x)), nums))
tmp, base = 1, 10
for i in range(max_len):
; Quick miniKanren-like code
;
; written at the meeting of a Functional Programming Group
; (Toukyou/Shibuya, Apr 29, 2006), as a quick illustration of logic
; programming. The code is really quite trivial and unsophisticated:
; it was written without any preparation whatsoever. The present file
; adds comments and makes minor adjustments.
;
; $Id: sokuza-kanren.scm,v 1.1 2006/05/10 23:12:41 oleg Exp oleg $
@m00nlight
m00nlight / gist:61839e7007f95d8c13b3
Created February 3, 2015 11:26
Functional style of quick sort in python
def quick_sort(nums):
if len(nums) <= 1:
return nums
else:
less = filter(lambda x: x <= nums[0], nums[1:])
large = filter(lambda x: x > nums[0], nums[1:])
return quick_sort(less) + [nums[0]] + quick_sort(large)
@m00nlight
m00nlight / gist:bfe54d1b2db362755a3a
Last active June 30, 2019 06:17
Python reservoir sampling algorithm
from random import randrange
def reservoir_sampling(items, k):
"""
Reservoir sampling algorithm for large sample space or unknow end list
See <http://en.wikipedia.org/wiki/Reservoir_sampling> for detail>
Type: ([a] * Int) -> [a]
Prev constrain: k is positive and items at least of k items
Post constrain: the length of return array is k
"""
@m00nlight
m00nlight / gist:b3f7a4e4fb9ee9ddd1dd
Last active April 18, 2018 10:13
Python Knuth shuffle algorithm
def knuth_shuffle(items):
"""
Fisher-Yates shuffle or Knuth shuffle which name is more famous.
See <http://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle> for detail
Type : [a] -> None (shuffle inplace)
Post constrain: Should be list
Post constrain: return array of the same length of input
"""
for i in range(len(items)):
@m00nlight
m00nlight / gist:f07689808e88a4fd125f
Last active August 29, 2015 14:14
Python count sort
def count_sort(A, K):
""" A is positive integer array each element is in range [0, K]"""
count = [0] * (K + 1)
for a in A:
count[a] += 1
for i in range(1, K + 1):
count[i] = count[i - 1] + count[i]
B = [None] * len(A)