|
| 1 | +<script setup lang="ts"> |
| 2 | +import Base64 from 'js-base64'; |
| 3 | +import hexArray from 'hex-array'; |
| 4 | +import { useCopy } from '@/composable/copy'; |
| 5 | +import { isValidBase64 } from '@/utils/base64'; |
| 6 | +
|
| 7 | +const textInput = ref(''); |
| 8 | +const base64Output = computed(() => { |
| 9 | + try { |
| 10 | + return Base64.fromUint8Array(hexArray.fromString(textInput.value)); |
| 11 | + } |
| 12 | + catch (e: any) { |
| 13 | + return e.toString(); |
| 14 | + } |
| 15 | +}); |
| 16 | +const { copy: copyTextBase64 } = useCopy({ source: base64Output, text: 'Base64 Hex Array copied to the clipboard' }); |
| 17 | +
|
| 18 | +const uppercase = ref(false); |
| 19 | +const grouping = ref(0); |
| 20 | +const rowlength = ref(0); |
| 21 | +
|
| 22 | +const base64Input = ref(''); |
| 23 | +const textOutput = computed(() => { |
| 24 | + try { |
| 25 | + return hexArray.toString(Base64.toUint8Array(base64Input.value), { |
| 26 | +
|
| 27 | + }); |
| 28 | + } |
| 29 | + catch (e: any) { |
| 30 | + return e.toString(); |
| 31 | + } |
| 32 | +}, |
| 33 | +); |
| 34 | +const { copy: copyText } = useCopy({ source: textOutput, text: 'Hex Array copied to the clipboard' }); |
| 35 | +const b64ValidationRules = [ |
| 36 | + { |
| 37 | + message: 'Invalid base64 string', |
| 38 | + validator: (value: string) => isValidBase64(value.trim()), |
| 39 | + }, |
| 40 | +]; |
| 41 | +</script> |
| 42 | + |
| 43 | +<template> |
| 44 | + <c-card title="Hex Array to Base64"> |
| 45 | + <c-input-text |
| 46 | + v-model:value="textInput" |
| 47 | + multiline |
| 48 | + placeholder="Put your Hex Array here..." |
| 49 | + rows="5" |
| 50 | + label="Hex Array to encode" |
| 51 | + raw-text |
| 52 | + mb-5 |
| 53 | + /> |
| 54 | + |
| 55 | + <c-input-text |
| 56 | + label="Base64 of Hex Array" |
| 57 | + :value="base64Output" |
| 58 | + multiline |
| 59 | + readonly |
| 60 | + placeholder="The base64 encoding of your Hex Array will be here" |
| 61 | + rows="5" |
| 62 | + mb-5 |
| 63 | + /> |
| 64 | + |
| 65 | + <div flex justify-center> |
| 66 | + <c-button @click="copyTextBase64()"> |
| 67 | + Copy base64 |
| 68 | + </c-button> |
| 69 | + </div> |
| 70 | + </c-card> |
| 71 | + |
| 72 | + <c-card title="Base64 to Hex Array"> |
| 73 | + <n-form-item label="Uppercase" label-placement="left"> |
| 74 | + <n-switch v-model:value="uppercase" /> |
| 75 | + </n-form-item> |
| 76 | + <n-form-item label="Group by" label-placement="left"> |
| 77 | + <n-input-number v-model:value="grouping" :min="0" /> digits (0 = no grouping) |
| 78 | + </n-form-item> |
| 79 | + <n-form-item label="Split as rows by" label-placement="left"> |
| 80 | + <n-input-number v-model:value="rowlength" :min="0" /> group of digits (0 = no rows) |
| 81 | + </n-form-item> |
| 82 | + <c-input-text |
| 83 | + v-model:value="base64Input" |
| 84 | + multiline |
| 85 | + placeholder="Your base64 Hex Array..." |
| 86 | + rows="5" |
| 87 | + :validation-rules="b64ValidationRules" |
| 88 | + label="Base64 Hex Array to decode" |
| 89 | + mb-5 |
| 90 | + /> |
| 91 | + |
| 92 | + <c-input-text |
| 93 | + v-model:value="textOutput" |
| 94 | + label="Decoded Hex Array" |
| 95 | + placeholder="The decoded Hex Array will be here" |
| 96 | + multiline |
| 97 | + rows="5" |
| 98 | + readonly |
| 99 | + mb-5 |
| 100 | + /> |
| 101 | + |
| 102 | + <div flex justify-center> |
| 103 | + <c-button @click="copyText()"> |
| 104 | + Copy decoded Hex Array |
| 105 | + </c-button> |
| 106 | + </div> |
| 107 | + </c-card> |
| 108 | +</template> |
0 commit comments