From 03b8717a967e8cff53495ee1f6a93f3980592301 Mon Sep 17 00:00:00 2001 From: Haakon Nilsen Date: Fri, 2 Dec 2011 23:59:15 +0100 Subject: [PATCH] first commit --- COPYING | 9 +++ README | 12 ++++ pom.xml | 42 ++++++++++++ src/main/scala/szbase32/Main.scala | 22 +++++++ src/main/scala/szbase32/ZBase32.scala | 95 +++++++++++++++++++++++++++ 5 files changed, 180 insertions(+) create mode 100644 COPYING create mode 100644 README create mode 100644 pom.xml create mode 100644 src/main/scala/szbase32/Main.scala create mode 100644 src/main/scala/szbase32/ZBase32.scala diff --git a/COPYING b/COPYING new file mode 100644 index 0000000..389ecba --- /dev/null +++ b/COPYING @@ -0,0 +1,9 @@ +The MIT License (MIT) + +Copyright (c) 2011 Haakon Nilsen + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/README b/README new file mode 100644 index 0000000..9e814cc --- /dev/null +++ b/README @@ -0,0 +1,12 @@ +szbase32 is an implementation of the z-base-32 encoding in Scala. + +This is basically the standard RFC 4648 Base32 encoding but with a custom +alphabet permutation to make the encodings easier to read and communicate, +and also leaves out padding to make the code shorter. + +Background: + +https://tools.ietf.org/html/rfc4648 +http://philzimmermann.com/docs/human-oriented-base-32-encoding.txt + +Feel free to use this under the terms of the MIT license. diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..f865fbc --- /dev/null +++ b/pom.xml @@ -0,0 +1,42 @@ + + 4.0.0 + + szbase32 + szbase32 + 1.0 + jar + szbase32 + + + UTF-8 + 2.9.1 + + + + + scala-tools.org + Scala-tools Maven2 Repository + http://scala-tools.org/repo-releases + + + + + + + org.scala-tools + maven-scala-plugin + 2.15.2 + + + + compile + testCompile + + + + + + + + diff --git a/src/main/scala/szbase32/Main.scala b/src/main/scala/szbase32/Main.scala new file mode 100644 index 0000000..5665a38 --- /dev/null +++ b/src/main/scala/szbase32/Main.scala @@ -0,0 +1,22 @@ +package szbase32 + +/** + * Basic CLI program that encodes strings to z-base-32, and decodes z-base-32 + * to UTF-8 strings. + */ +object Main extends App { + + def usage = println("Usage: zbase32.Main [encode|decode] [input]") + + if (args.length < 2) { + usage + } else { + val in = args slice(1, args.length) mkString " " + println(args(0) match { + case "encode" => ZBase32.encode(in getBytes) + case "decode" => new String(ZBase32.decode(in), "UTF-8") + case _ => usage + }) + } + +} diff --git a/src/main/scala/szbase32/ZBase32.scala b/src/main/scala/szbase32/ZBase32.scala new file mode 100644 index 0000000..50be239 --- /dev/null +++ b/src/main/scala/szbase32/ZBase32.scala @@ -0,0 +1,95 @@ +package szbase32 + +// TODO: don't break on invalid z-base-32 input. + +import java.io.ByteArrayOutputStream +import java.io.OutputStream + +/** + * Implements the + * z-base-32 encoding. + * + * @author Haakon Nilsen + */ +object ZBase32 { + + val encTable = "ybndrfg8ejkmcpqxot1uwisza345h769".toList + val decTable = encTable.map(c => (c, encTable indexOf c)).toMap + + def encode(src: Seq[Byte]) = { + val oddLength = src.size % 5 + val evenLength = src.size - oddLength + val buf = new StringBuilder + def d(x : Int) = src(x) & 0xff + def add(b: Int) = buf += encTable(b & 0x1f) + for (i <- 0 until evenLength by 5) { + val b1 = d(i) + val b2 = d(i + 1) + val b3 = d(i + 2) + val b4 = d(i + 3) + val b5 = d(i + 4) + add(b1 >> 3) + add((b1 << 2) | (b2 >> 6)) + add(b2 >> 1) + add((b2 << 4) | (b3 >> 4)) + add((b3 << 1) | (b4 >> 7)) + add(b4 >> 2) + add((b4 << 3) | (b5 >> 5)) + add(b5) + } + if (oddLength > 0) { + val b1 = d(evenLength) + lazy val b2 = d(evenLength + 1) + lazy val b3 = d(evenLength + 2) + add(b1 >> 3) + if (oddLength == 1) add(b1 << 2) + if (oddLength > 1) { + add((b1 << 2) | (b2 >> 6)) + add(b2 >> 1) + } + if (oddLength == 2) add(b2 << 4) + if (oddLength > 2) add((b2 << 4) | (b3 >> 4)) + if (oddLength == 3) add(b3 << 1) + if (oddLength == 4) { + val b4 = d(evenLength + 3) + add((b3 << 1) | (b4 >> 7)) + add(b4 >> 2) + add(b4 << 3) + } + } + buf.toString + } + + def decode(src: String) = { + /*We allow whitespace and dashes in the z-base-32 input, but remove + * it before decoding:*/ + val d = src.replaceAll("([\\s-])", "") + val out = new ByteArrayOutputStream + val oddLength = d.size % 8 + val evenLength = d.size - oddLength - 1 + for (i <- 0 to evenLength by 8) { + val b = Range(0, 8).map(j => j -> decTable(d(i + j))).toMap + out.write((b(0) << 3) | (b(1) >> 2)) + out.write((b(1) << 6) | (b(2) << 1) | (b(3) >> 4)) + out.write((b(3) << 4) | (b(4) >> 1)) + out.write((b(4) << 7) | (b(5) << 2) | (b(6) >> 3)) + out.write((b(6) << 5) | b(7)) + } + if (oddLength > 1) { // oddLength ∈ {2,4,5,7} + val e = evenLength + 1 + def b(x: Int) = decTable(d(x)) + out.write((b(e) << 3) | (b(e + 1) >> 2)) + if (oddLength > 3) { + out.write((b(e + 1) << 6) | (b(e + 2) << 1) | (b(e + 3) >> 4)) + } + if (oddLength > 4) { + out.write((b(e + 3) << 4) | (b(e + 4) >> 1)) + } + if (oddLength > 6) { + out.write((b(e + 4) << 7) | (b(e + 5) << 2) | (b(e + 6) >> 3)) + } + } + out.toByteArray + } + +}