Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
briar
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
680
Issues
680
List
Boards
Labels
Service Desk
Milestones
Merge Requests
11
Merge Requests
11
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Operations
Operations
Incidents
Environments
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
briar
briar
Commits
fc8ca872
Verified
Commit
fc8ca872
authored
Apr 17, 2019
by
akwizgran
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add base32 encoder/decoder.
parent
6f0ab8b6
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
75 additions
and
0 deletions
+75
-0
bramble-api/src/main/java/org/briarproject/bramble/util/Base32.java
...i/src/main/java/org/briarproject/bramble/util/Base32.java
+75
-0
No files found.
bramble-api/src/main/java/org/briarproject/bramble/util/Base32.java
0 → 100644
View file @
fc8ca872
package
org.briarproject.bramble.util
;
import
java.io.ByteArrayOutputStream
;
public
class
Base32
{
private
static
final
char
[]
DIGITS
=
{
'A'
,
'B'
,
'C'
,
'D'
,
'E'
,
'F'
,
'G'
,
'H'
,
'I'
,
'J'
,
'K'
,
'L'
,
'M'
,
'N'
,
'O'
,
'P'
,
'Q'
,
'R'
,
'S'
,
'T'
,
'U'
,
'V'
,
'W'
,
'X'
,
'Y'
,
'Z'
,
'2'
,
'3'
,
'4'
,
'5'
,
'6'
,
'7'
};
public
static
String
encode
(
byte
[]
b
)
{
StringBuilder
s
=
new
StringBuilder
();
int
byteIndex
=
0
,
currentCode
=
0x00
;
int
byteMask
=
0x80
,
codeMask
=
0x10
;
while
(
byteIndex
<
b
.
length
)
{
if
((
b
[
byteIndex
]
&
byteMask
)
!=
0
)
currentCode
|=
codeMask
;
// After every 8 bits, move on to the next byte
if
(
byteMask
==
0x01
)
{
byteMask
=
0x80
;
byteIndex
++;
}
else
{
byteMask
>>>=
1
;
}
// After every 5 bits, move on to the next digit
if
(
codeMask
==
0x01
)
{
s
.
append
(
DIGITS
[
currentCode
]);
codeMask
=
0x10
;
currentCode
=
0x00
;
}
else
{
codeMask
>>>=
1
;
}
}
// If we're part-way through a digit, output it
if
(
codeMask
!=
0x10
)
s
.
append
(
DIGITS
[
currentCode
]);
return
s
.
toString
();
}
public
static
byte
[]
decode
(
String
s
)
{
ByteArrayOutputStream
b
=
new
ByteArrayOutputStream
();
int
digitIndex
=
0
,
digitCount
=
s
.
length
(),
currentByte
=
0x00
;
int
byteMask
=
0x80
,
codeMask
=
0x10
;
while
(
digitIndex
<
digitCount
)
{
int
code
=
decodeDigit
(
s
.
charAt
(
digitIndex
));
if
((
code
&
codeMask
)
!=
0
)
currentByte
|=
byteMask
;
// After every 8 bits, move on to the next byte
if
(
byteMask
==
0x01
)
{
b
.
write
(
currentByte
);
byteMask
=
0x80
;
currentByte
=
0x00
;
}
else
{
byteMask
>>>=
1
;
}
// After every 5 bits, move on to the next digit
if
(
codeMask
==
0x01
)
{
codeMask
=
0x10
;
digitIndex
++;
}
else
{
codeMask
>>>=
1
;
}
}
// If any extra bits were used for encoding, they should all be zero
if
(
byteMask
!=
0x80
&&
currentByte
!=
0x00
)
throw
new
IllegalArgumentException
();
return
b
.
toByteArray
();
}
private
static
int
decodeDigit
(
char
c
)
{
if
(
c
>=
'A'
&&
c
<=
'Z'
)
return
c
-
'A'
;
if
(
c
>=
'a'
&&
c
<=
'z'
)
return
c
-
'a'
;
if
(
c
>=
'2'
&&
c
<=
'7'
)
return
c
-
'2'
+
26
;
throw
new
IllegalArgumentException
(
"Not a base32 digit: "
+
c
);
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment