forked from haakonn/szbase32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 03b8717
Showing
5 changed files
with
180 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<groupId>szbase32</groupId> | ||
<artifactId>szbase32</artifactId> | ||
<version>1.0</version> | ||
<packaging>jar</packaging> | ||
<name>szbase32</name> | ||
|
||
<properties> | ||
<encoding>UTF-8</encoding> | ||
<scala.version>2.9.1</scala.version> | ||
</properties> | ||
|
||
<pluginRepositories> | ||
<pluginRepository> | ||
<id>scala-tools.org</id> | ||
<name>Scala-tools Maven2 Repository</name> | ||
<url>http://scala-tools.org/repo-releases</url> | ||
</pluginRepository> | ||
</pluginRepositories> | ||
|
||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.scala-tools</groupId> | ||
<artifactId>maven-scala-plugin</artifactId> | ||
<version>2.15.2</version> | ||
<executions> | ||
<execution> | ||
<goals> | ||
<goal>compile</goal> | ||
<goal>testCompile</goal> | ||
</goals> | ||
</execution> | ||
</executions> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
|
||
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
}) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 <a href="http://philzimmermann.com/docs/human-oriented-base-32-encoding.txt"> | ||
* z-base-32 encoding</a>. | ||
* | ||
* @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 | ||
} | ||
|
||
} |