Created
October 7, 2014 22:47
-
-
Save micahwalter/d6c8f8bc677978cf01a5 to your computer and use it in GitHub Desktop.
base58.sql
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
DELIMITER $$ | |
CREATE FUNCTION base58_encode (num bigint(64)) RETURNS varchar(255) | |
DETERMINISTIC | |
BEGIN | |
DECLARE alphabet varchar(255); | |
DECLARE base_count int DEFAULT 0; | |
DECLARE encoded varchar(255); | |
DECLARE divisor DECIMAL(10,4); | |
DECLARE mode int DEFAULT 0; | |
SET alphabet = '123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ'; | |
SET base_count = CHAR_LENGTH(alphabet); | |
SET encoded = ""; | |
WHILE num >= base_count DO | |
SET divisor = num / base_count; | |
SET mode = (num - (base_count* TRUNCATE(divisor,0))); | |
SET encoded = CONCAT(SUBSTRING(alphabet FROM mode+1 FOR 1), encoded); | |
SET num = TRUNCATE(divisor,0); | |
END WHILE; | |
SET encoded = CONCAT(SUBSTRING(alphabet FROM num+1 FOR 1), encoded); | |
RETURN (encoded); | |
END |
do you have decoder version?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
PostgreSQL Version: http://stackoverflow.com/a/34759277/1433665