PHPackages                             cardano-php/bech32 - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. cardano-php/bech32

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

cardano-php/bech32
==================

A Bech32 encoder/decoder for Cardano ecosystem assets

v1.1.0(1y ago)21.5k↓50%[6 issues](https://github.com/cardano-php/bech32/issues)Apache-2.0PHPPHP ^8.2CI passing

Since Jan 3Pushed 1y agoCompare

[ Source](https://github.com/cardano-php/bech32)[ Packagist](https://packagist.org/packages/cardano-php/bech32)[ RSS](/packages/cardano-php-bech32/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (2)Dependencies (1)Versions (4)Used By (0)

Bech32 Encoder/Decoder for Cardano
==================================

[](#bech32-encoderdecoder-for-cardano)

This library provides a pure PHP implementation of a Bech32 Encoder/Decoder tailored for use with the Cardano blockchain. It allows developers to seamlessly encode and decode Bech32 strings, which are widely used in Cardano addresses and other blockchain-related data.

Features
--------

[](#features)

- **Pure PHP Implementation**: No external dependencies, making it lightweight and easy to integrate.
- **Cardano-Specific**: Designed to meet the encoding and decoding requirements of the Cardano blockchain.
- **Simple API**: Intuitive methods for both encoding and decoding.

Installation
------------

[](#installation)

To install the library, use Composer:

```
composer require cardano-php/bech32
```

Alternatively, you can download the source code and include it manually in your project:

```
require_once 'path/to/Bech32.php';
```

Usage
-----

[](#usage)

### Encoding

[](#encoding)

```
use PhpCardano\Bech32\Bech32;

// Set up our data to be encoded and our human readable part (HRP)
$data = 'does-the-php-dev';
$hrp = 'cardanophp';
echo 'HRP: ' . $hrp . "\n" . ' Data: ' . $data . "\n";

// Hex-encode our data payload
$hexdata = bin2hex($data);
echo 'Hex Data: ' . $hexdata . "\n";

// Convert our hex-encoded payload to a 5-bit "byte" array
$bitdata = Bech32::hexToByteArray($hexdata);

// Bech32 encode the HRP + 5-bit data payload
$encoded = Bech32::encode($hrp, $bitdata);
echo 'Encoded: ' . $encoded . "\n";
```

Expected Output:

```
HRP: cardanophp
Data: does-the-php-dev
Hex Data: 646f65732d7468652d7068702d646576
Encoded: cardanophp1v3hk2uedw35x2ttsdpcz6er9wcjv4g3u

```

### Decoding

[](#decoding)

```
use PhpCardano\Bech32\Bech32;

$bech32 = 'cardanophp1v3hk2uedw35x2ttsdpcz6er9wcjv4g3u'; // Your Bech32 string

// Decode returns the human readable part (HRP) and the data payload in a 5-bit array
[$hrp, $bitData] = Bech32::decode($bech32);

// Convert the 5-bit data back to a hex string
$hexData = Bech32::byteArrayToHex($data);

echo 'HRP: ' . $hrp . "\n";
echo 'Hex Data: ' . $hexData . "\n";

// Convert (if needed) from hex back to binary/ASCII

$plainData = hex2bin($hexData);

echo 'Data Payload: ' . $plainData . "\n";
```

Expected Output

```
HRP: cardanophp
Hex Data: 646f65732d7468652d7068702d646576
Data Payload: does-the-php-dev

```

### Decoding a Cardano Address

[](#decoding-a-cardano-address)

Example PHP Code:

```
/**
* JPG v2 Staked Public Contract
**/

use CardanoPhp\Bech32\Bech32;

$bech32String = 'addr1qxegfu8m62peqmyamrdwmwqm00zjcak3u25xnanfdct4p9pf488uagw68fv50kjxv3wrx38829tay6zszthnccsradgqwt4upy';
$result = Bech32::decodeCardanoAddress($bech32String);
echo "Result:\n".json_encode($result, JSON_PRETTY_PRINT)."\n";
```

Output:

```
Result:
{
    "address": "addr1xxgx3far7qygq0k6epa0zcvcvrevmn0ypsnfsue94nsn3tfvjel5h55fgjcxgchp830r7h2l5msrlpt8262r3nvr8eks2utwdd",
    "addressType": 3,
    "networkId": 1,
    "paymentHash": "9068a7a3f008803edac87af1619860f2cdcde40c26987325ace138ad",
    "stakingHash": "2c967f4bd28944b06462e13c5e3f5d5fa6e03f8567569438cd833e6d",
    "stakeAddress": "stake17ykfvl6t62y5fvryvtsnch3lt406dcpls4n4d9pcekpnumg6v83tq"
}

```

### Encoding Cardano Address

[](#encoding--cardano-address)

Example PHP Code:

```
/**
* JPG v2 Staked Public Contract
**/

use CardanoPhp\Bech32\Bech32;

$addressType = 3;                                                          // Script Hash + Script Hash
$networkId = 1;                                                            // Cardano Mainnet
$paymentHash = '9068a7a3f008803edac87af1619860f2cdcde40c26987325ace138ad'; // Payment Validator
$stakeHash = '2c967f4bd28944b06462e13c5e3f5d5fa6e03f8567569438cd833e6d';   // Stake Validator

$address = Bech32::encodeCardanoAddress($addressType, $networkId, $paymentHash, $stakeHash);
echo "Result:\n" . json_encode($address, JSON_PRETTY_PRINT) . "\n";
```

```
Result:
{
    "address": "addr1xxgx3far7qygq0k6epa0zcvcvrevmn0ypsnfsue94nsn3tfvjel5h55fgjcxgchp830r7h2l5msrlpt8262r3nvr8eks2utwdd",
    "addressType": 3,
    "networkId": 1,
    "paymentHash": "9068a7a3f008803edac87af1619860f2cdcde40c26987325ace138ad",
    "stakeHash": "2c967f4bd28944b06462e13c5e3f5d5fa6e03f8567569438cd833e6d",
    "stakeAddress": "stake17ykfvl6t62y5fvryvtsnch3lt406dcpls4n4d9pcekpnumg6v83tq"
}

```

### Error Handling

[](#error-handling)

Ensure to handle potential exceptions when decoding invalid Bech32 strings:

```
try {
    [$hrp, $data] = Bech32::decode($bech32);
} catch (Exception $e) {
    echo "Decoding failed: " . $e->getMessage();
}
```

Requirements
------------

[](#requirements)

- PHP 8.2 or later

Tests
-----

[](#tests)

Run the tests to ensure the library works as expected:

```
composer test
```

> Note: This will also generate code coverage reports that can be viewed in the `./coverage` folder.

Contributing
------------

[](#contributing)

Contributions are welcome! Feel free to fork the repository and submit a pull request. Ensure to include tests for any new features or bug fixes.

License
-------

[](#license)

This project is licensed under the Apache 2.0 License. See the [LICENSE](LICENSE) file for details.

Acknowledgments
---------------

[](#acknowledgments)

This library was inspired by the Bech32 implementation originally by [Bit-Wasp Bech32 Implementation](https://github.com/Bit-Wasp/bech32) and Cardano [CIP-0019](https://github.com/cardano-foundation/CIPs/blob/master/CIP-0019/README.md).

###  Health Score

28

—

LowBetter than 54% of packages

Maintenance21

Infrequent updates — may be unmaintained

Popularity21

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity53

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~4 days

Total

2

Last Release

496d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/f9a030c2a0aefc298a1c077574477b277cc587b73bb2ec9bfefbe1713f051eb2?d=identicon)[Crypto2099](/maintainers/Crypto2099)

---

Top Contributors

[![Crypto2099](https://avatars.githubusercontent.com/u/63186174?v=4)](https://github.com/Crypto2099 "Crypto2099 (20 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/cardano-php-bech32/health.svg)

```
[![Health](https://phpackages.com/badges/cardano-php-bech32/health.svg)](https://phpackages.com/packages/cardano-php-bech32)
```

###  Alternatives

[symfony/config

Helps you find, load, combine, autofill and validate configuration values of any kind

4.3k455.2M6.6k](/packages/symfony-config)[thecodingmachine/safe

PHP core functions that throw exceptions instead of returning FALSE on error

2.5k95.7M455](/packages/thecodingmachine-safe)[matthiasmullie/minify

CSS &amp; JavaScript minifier, in PHP. Removes whitespace, strips comments, combines files (incl. @import statements and small assets in CSS files), and optimizes/shortens a few common programming patterns.

2.0k30.5M336](/packages/matthiasmullie-minify)[nette/robot-loader

🍀 Nette RobotLoader: high performance and comfortable autoloader that will search and autoload classes within your application.

89352.7M321](/packages/nette-robot-loader)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
