Skip to content

Instantly share code, notes, and snippets.

@mtigas
Last active December 20, 2015 21:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mtigas/6201261 to your computer and use it in GitHub Desktop.
Save mtigas/6201261 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
#coding=utf-8
"""
btsync_key.py
generates keys of abitrary size, for bittorrent sync[1].
btsync uses a 32-char key as the secret identifier for a shared folder,
but this key actually be a Base64 string of any length > 40.[2] so
this script allows you to generate stronger keys than otherwise
possible.
[1]: http://labs.bittorrent.com/experiments/sync.html
[2]: http://labs.bittorrent.com/experiments/sync/get-started.html#secretKeyAdvanced
-----
Copyright 2010-2013, Mike Tigas
https://mike.tig.as/
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""
from __future__ import division
from random import SystemRandom
import struct
from base64 import b32encode
KEYSIZE = 40 * 8
STRSPLIT = 4
if __name__ == "__main__":
random=SystemRandom()
print "Generating key of %d bytes (%d bits)..." % (KEYSIZE/8, KEYSIZE)
key = ""
keylen = 0
while (keylen < KEYSIZE):
key += struct.pack("B", random.getrandbits(8))
keylen += 8
k32 = b32encode(key)
print
print "Raw:\n%s" % key
print "\nRepr:\n%s" % repr(key)
print "\nB32:\n%s" % k32
k32_split = ""
z = 0
while (z < KEYSIZE):
k32_split += ((k32[z:z+STRSPLIT]) + " ")
z += STRSPLIT
print k32_split.strip()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment