Skip to content

Latest commit

 

History

History
69 lines (58 loc) · 2.09 KB

File metadata and controls

69 lines (58 loc) · 2.09 KB

Module base32

Base32 encoding/decoding in accordance with Crockford, RFC 4648 section 6, RFC 4648 section 7

val crockford = Base32.Crockford.Builder {
    isLenient(enable = true)
    encodeLowercase(enable = false)
    hyphen(interval = 5)
    check(symbol = '~')
    backFillBuffers(enable = true)
}

val text = "Hello World!"
val bytes = text.encodeToByteArray()
val encoded = bytes.encodeToString(crockford)
println(encoded) // 91JPR-V3F41-BPYWK-CCGGG~

// Alternatively, use the static implementation containing
// pre-configured settings, instead of creating your own.
val decoded = encoded.decodeToByteArray(Base32.Crockford).decodeToString()
assertEquals(text, decoded)
val default = Base32.Default.Builder {
    isLenient(enable = true)
    lineBreak(interval = 64)
    lineBreakReset(onFlush = true)
    encodeLowercase(enable = false)
    padEncoded(enable = true)
    backFillBuffers(enable = true)
}

val text = "Hello World!"
val bytes = text.encodeToByteArray()
val encoded = bytes.encodeToString(default)
println(encoded) // JBSWY3DPEBLW64TMMQQQ====

// Alternatively, use the static implementation containing
// pre-configured settings, instead of creating your own.
val decoded = encoded.decodeToByteArray(Base32.Default).decodeToString()
assertEquals(text, decoded)
val hex = Base32.Hex.Builder {
    isLenient(enable = true)
    lineBreak(interval = 64)
    lineBreakReset(onFlush = true)
    encodeLowercase(enable = false)
    padEncoded(enable = true)
    backFillBuffers(enable = true)
}

val text = "Hello World!"
val bytes = text.encodeToByteArray()
val encoded = bytes.encodeToString(hex)
println(encoded) // 91IMOR3F41BMUSJCCGGG====

// Alternatively, use the static implementation containing
// pre-configured settings, instead of creating your own.
val decoded = encoded.decodeToByteArray(Base32.Hex).decodeToString()
assertEquals(text, decoded)