Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
151 changes: 76 additions & 75 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -1,84 +1,85 @@
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.github.multiformats</groupId>
<artifactId>java-multibase</artifactId>
<version>v1.2.0</version>
<packaging>jar</packaging>
<groupId>com.github.multiformats</groupId>
<artifactId>java-multibase</artifactId>
<version>v1.3.0-SNAPSHOT</version>
<packaging>jar</packaging>

<name>multibase</name>
<url>https://github.com/multiformats/java-multibase</url>
<name>multibase</name>
<url>https://github.com/multiformats/java-multibase</url>

<issueManagement>
<url>https://github.com/multiformats/java-multibase/issues</url>
<system>GitHub Issues</system>
</issueManagement>
<issueManagement>
<url>https://github.com/multiformats/java-multibase/issues</url>
<system>GitHub Issues</system>
</issueManagement>

<scm>
<url>https://github.com/multiformats/java-multibase</url>
<connection>scm:git:git://github.com/multiformats/java-multibase.git</connection>
<developerConnection>scm:git:git@github.com:multiformats/java-multibase.git</developerConnection>
</scm>
<scm>
<url>https://github.com/multiformats/java-multibase</url>
<connection>scm:git:git://github.com/multiformats/java-multibase.git</connection>
<developerConnection>scm:git:git@github.com:multiformats/java-multibase.git</developerConnection>
</scm>

<licenses>
<license>
<name>MIT License</name>
<url>https://github.com/multiformats/java-multiaddr/blob/master/LICENSE</url>
<distribution>repo</distribution>
</license>
</licenses>
<licenses>
<license>
<name>MIT License</name>
<url>https://github.com/multiformats/java-multiaddr/blob/master/LICENSE</url>
<distribution>repo</distribution>
</license>
</licenses>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<version.junit>5.11.0</version.junit>
<version.hamcrest>3.0</version.hamcrest>
</properties>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<version.junit>5.11.0</version.junit>
<version.hamcrest>3.0</version.hamcrest>
</properties>

<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>${version.junit}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest</artifactId>
<version>${version.hamcrest}</version>
<scope>test</scope>
</dependency>
</dependencies>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>${version.junit}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest</artifactId>
<version>${version.hamcrest}</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>11</source>
<target>11</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.3.1</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
<configuration>
<archive>
<manifest>
<addDefaultImplementationEntries>true</addDefaultImplementationEntries>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>11</source>
<target>11</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.3.1</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
<configuration>
<archive>
<manifest>
<addDefaultImplementationEntries>true</addDefaultImplementationEntries>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
</project>
103 changes: 103 additions & 0 deletions src/main/java/io/ipfs/multibase/Base256Emoji.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package io.ipfs.multibase;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

/*
* Copyright 2025 Michael Vorburger.ch
*
* 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.
*/

/**
* <a href="https://github.com/multiformats/multibase/blob/master/rfcs/Base256Emoji.md">Base256Emoji</a>
* is an encoding mapping each 0-255 byte value to (or from) a specific single Unicode Emoji character.
*
* @author <a href="https://www.vorburger.ch/">Michael Vorburger.ch</a>
*/
public class Base256Emoji {

// from https://github.com/multiformats/multibase/blob/master/rfcs/Base256Emoji.md
private static final String[] EMOJIS = {
"🚀", "🪐", "☄", "🛰", "🌌", "🌑", "🌒", "🌓", "🌔", "🌕",
"🌖", "🌗", "🌘", "🌍", "🌏", "🌎", "🐉", "☀", "💻", "🖥",
"💾", "💿", "😂", "❤", "😍", "🤣", "😊", "🙏", "💕", "😭",
"😘", "👍", "😅", "👏", "😁", "🔥", "🥰", "💔", "💖", "💙",
"😢", "🤔", "😆", "🙄", "💪", "😉", "☺", "👌", "🤗", "💜",
"😔", "😎", "😇", "🌹", "🤦", "🎉", "💞", "✌", "✨", "🤷",
"😱", "😌", "🌸", "🙌", "😋", "💗", "💚", "😏", "💛", "🙂",
"💓", "🤩", "😄", "😀", "🖤", "😃", "💯", "🙈", "👇", "🎶",
"😒", "🤭", "❣", "😜", "💋", "👀", "😪", "😑", "💥", "🙋",
"😞", "😩", "😡", "🤪", "👊", "🥳", "😥", "🤤", "👉", "💃",
"😳", "✋", "😚", "😝", "😴", "🌟", "😬", "🙃", "🍀", "🌷",
"😻", "😓", "⭐", "✅", "🥺", "🌈", "😈", "🤘", "💦", "✔",
"😣", "🏃", "💐", "☹", "🎊", "💘", "😠", "☝", "😕", "🌺",
"🎂", "🌻", "😐", "🖕", "💝", "🙊", "😹", "🗣", "💫", "💀",
"👑", "🎵", "🤞", "😛", "🔴", "😤", "🌼", "😫", "⚽", "🤙",
"☕", "🏆", "🤫", "👈", "😮", "🙆", "🍻", "🍃", "🐶", "💁",
"😲", "🌿", "🧡", "🎁", "⚡", "🌞", "🎈", "❌", "✊", "👋",
"😰", "🤨", "😶", "🤝", "🚶", "💰", "🍓", "💢", "🤟", "🙁",
"🚨", "💨", "🤬", "✈", "🎀", "🍺", "🤓", "😙", "💟", "🌱",
"😖", "👶", "🥴", "▶", "➡", "❓", "💎", "💸", "⬇", "😨",
"🌚", "🦋", "😷", "🕺", "⚠", "🙅", "😟", "😵", "👎", "🤲",
"🤠", "🤧", "📌", "🔵", "💅", "🧐", "🐾", "🍒", "😗", "🤑",
"🌊", "🤯", "🐷", "☎", "💧", "😯", "💆", "👆", "🎤", "🙇",
"🍑", "❄", "🌴", "💣", "🐸", "💌", "📍", "🥀", "🤢", "👅",
"💡", "💩", "👐", "📸", "👻", "🤐", "🤮", "🎼", "🥵", "🚩",
"🍎", "🍊", "👼", "💍", "📣", "🥂" };

// TODO Propose adding a Guava dependency to use ImmutableMap instead of this

private static final Map<String, Integer> EMOJI_TO_INDEX;
private static final int MAP_EXPECTED_SIZE = EMOJIS.length;
private static final float MAP_LOAD_FACTOR = 1.0f;

static {
if (EMOJIS.length != 256) {
throw new IllegalStateException("EMOJIS.length must be 256, but is " + EMOJIS.length);
}

Map<String, Integer> mutableMap = new HashMap<>(MAP_EXPECTED_SIZE, MAP_LOAD_FACTOR);
for (int i = 0; i < EMOJIS.length; i++) {
mutableMap.put(EMOJIS[i], i);
}
EMOJI_TO_INDEX = Collections.unmodifiableMap(mutableMap);
}

public static String encode(byte[] in) {
StringBuilder sb = new StringBuilder(in.length);
for (byte b : in) {
sb.append(EMOJIS[b & 0xFF]);
}
return sb.toString();
}

public static byte[] decode(String in) {
int length = in.codePointCount(0, in.length());
byte[] bytes = new byte[length];

for (int i = 0; i < in.codePointCount(0, in.length()); i++) {
int cp = in.codePointAt(in.offsetByCodePoints(0, i));
String emoji = new String(Character.toChars(cp));
Integer index = EMOJI_TO_INDEX.get(emoji);
if (index == null) {
throw new IllegalArgumentException("Unknown Base256Emoji character: " + emoji);
}
bytes[i] = (byte) (index & 0xFF);
}

return bytes;
}

}
Loading