libslug 0.12.0

A Rust Library For Cryptography Intended For Slug20 That Supports X59 Certificate Format and Post-Quantum Cryptography
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
//! # ED25519 Digital Signature
//! 
//! This contains the ED25519 Digital Signature scheme using dalek's audited crate. It implements zeroize, serialization, and other common utilties and makes it easy to sign, as well as store keys.
//! 
//! ## Features
//! 
//! ## Verified Implemented Traits
//! 
//! - [X] FromEncoding (TODO: Fix Errors)
//! - [X] IntoEncoding
//! 
//! 
//! ### Generation
//! 
//! - [X] Operating System Randomness
//! - [X] SecureRand
//! - [X] SecureRand with determinstic generation
//! - [X] BIP39
//! 
//! ### Signing
//! 
//! - [X] Sign
//! - [ ] Sign with Hedged Signatures
//! 
//! ### Verification
//! 
//! - [X] Verify
//! 
//! ### Encodings
//! 
//! Supports multiple encodings, including hexadecimal, base32, and base58.
//! 
//! ## TODO
//! 
//! - PKCS #7
//! - More encodings
//! - Certificate Encoding

use std::str::FromStr;

use bip39::Language;
use ed25519_dalek::{Signer,Verifier};
use ed25519_dalek::ed25519::SignatureEncoding;
use ed25519_dalek::SignatureError;
use ed25519_dalek::SigningKey;
use ed25519_dalek::VerifyingKey;
use ed25519_dalek::Signature;
use ed25519_dalek::SecretKey;
use rand::rngs::OsRng;
use zeroize::{Zeroize,ZeroizeOnDrop};
use serde::{Serialize,Deserialize};
use crate::slugcrypt::internals::bip39::SlugMnemonic;
use crate::slugcrypt::internals::csprng::SlugCSPRNG;
use crate::errors::SlugErrors;
use subtle_encoding::hex;
use subtle_encoding::Error;

use slugencode::prelude::*;

use bip39::ErrorKind;

use base32;
use base58::{FromBase58,ToBase58,FromBase58Error};
use serde_big_array::BigArray;
use slugencode::prelude::*;
use pem::Pem;

use crate::slugcrypt::traits::{IntoEncoding, IntoPemPublic, IntoPemSecret, IntoPemSignature,FromEncoding};
use crate::slugcrypt::traits::{IntoX59PublicKey,IntoX59SecretKey,IntoX59Signature};
use crate::slugcrypt::traits::{FromBincode,IntoBincode};
use crate::slugcrypt::traits::{FromStandardPem, IntoStandardPem};
use crate::slugcrypt::traits::{IntoStandardEncoding,FromStandardEncoding};


/// # ED25519: Public Key (Verifying Key)
/// 
/// ## Description
/// 
/// 32-byte Key in ED25519. It implements zeroize and serialization.
/// 
/// It is used to verify signatures.
#[derive(Zeroize,ZeroizeOnDrop,Serialize,Deserialize, Clone, Debug, PartialEq, PartialOrd, Hash)]
pub struct ED25519PublicKey([u8;32]);

/// # ED25519: Secret Key (Signing Key)
/// 
/// ## Description
/// 
/// 32-byte Key in ED25519. It implements zeroize and serialization.
/// 
/// It is used to sign data/messages.
/// 
/// The public key can be derived from the secret key.
/// 
/// ## Generation
/// 
/// - From Operating System Randomness (OS)
/// - SecureRand (Ephermal Password + Argon2id + ChaCha20RNG + OSCSPRNG Salt)
/// - BIP39
/// 
/// ## Signing
/// 
/// - Sign message or data
#[derive(Zeroize,ZeroizeOnDrop,Serialize,Deserialize, Clone, Debug, PartialEq, PartialOrd, Hash)]
pub struct ED25519SecretKey([u8;32]);

/// # ED25519: Signature
/// 
/// ## Description
/// 
/// 64-byte signature in ED25519. It implements zeroize and serialization.
/// 
/// It is used to verify digital signatures.
#[derive(Zeroize,ZeroizeOnDrop,Debug,Serialize,Deserialize, Clone, PartialEq, PartialOrd, Hash)]
pub struct ED25519Signature(#[serde(with = "BigArray")][u8;64]);


pub mod protocol_info {
    pub const PROTOCOL_NAME: &str = "libslug20/ed25519";
    pub const PK_SIZE: usize = 32;
    pub const SK_SIZE: usize = 32;
    pub const SIG_SIZE: usize = 64;
    pub const DERIVES_PUBLIC_KEY_FROM_SECRET: bool = true;
    pub const RANDOMNESS: [&str;4] = ["Operating-System CSPRNG","SecureRand","Deterministic With Password","BIP39"];
    pub const ENCODINGS: [&str;6] = ["Hexadecimal (Upper)","Base32 (Crockford)","Base58","PEM","Base64","Base64 URL Safe"];
}

impl ED25519SecretKey {
    /// Generates from OS-Generated Random Seed
    /// 
    /// ```rust
    /// fn main() {
    ///     use libslug::slugcrypt::internals::signature::ed25519;
    /// 
    ///     // Generate Secret Key From Operating System Randomness
    ///     let sk = ed25519::ED25519SecretKey::generate();
    /// }
    /// ```
    pub fn generate() -> ED25519SecretKey {
        let csprng = SlugCSPRNG::os_rand();
        let signing_key = SigningKey::from_bytes(&csprng);

        return ED25519SecretKey(signing_key.to_bytes())
    }
    /// Generates ED25519 Secret Key From Password With OS-Generated Salt using Argon2id and pushes into ChaCha20RNG to generate seed.
    pub fn generate_securerand(pass: &str) -> ED25519SecretKey {
        let csprng = SlugCSPRNG::new(pass);
        let signing_key = SigningKey::from_bytes(&csprng);

        return ED25519SecretKey(signing_key.to_bytes())
    }
    /// \[Determinstic] Generates Determinstically (warning: some security caution as this is deterministic with password and salt)
    pub fn generate_deterministic(pass: &str, salt: &str) -> ED25519SecretKey {
        let csprng = SlugCSPRNG::derive_from_password_with_salt(pass, salt);
        let signing_key = SigningKey::from_bytes(&csprng);

        return ED25519SecretKey(signing_key.to_bytes());
    }
    /// From BIP39 (Generation or From)
    pub fn from_bip39(mnemonic: SlugMnemonic, password: &str) -> Result<Self,ErrorKind> {
        let seed = mnemonic.to_seed(password)?;
        Ok(Self::from_bytes(&seed).unwrap())
    }
    /// to byte array of 32 bytes
    pub fn to_bytes(&self) -> [u8;32] {
        self.0
    }
    /// as bytes
    pub fn as_bytes(&self) -> &[u8] {
        &self.0
    }
    pub fn to_vec(&self) -> Vec<u8> {
        self.0.to_vec()
    }
    /// [Encoding] UPPER-HEXADECIMAL
    pub fn to_hex_string(&self) -> String {
        String::from_utf8(hex::encode_upper(self.0)).unwrap()
    }
    /// [Encoding] Decode From UPPER-HEXADECIMAL
    pub fn from_hex_string<T: AsRef<str>>(hex_str: T) -> Result<Vec<u8>,Error> {
        let bytes = hex::decode_upper(hex_str.as_ref().as_bytes())?;
        Ok(bytes)
    }
    /// from bytes
    pub fn from_bytes(bytes: &[u8]) -> Result<ED25519SecretKey, SlugErrors> {
        let mut secret_key_array: [u8;32] = [0u8;32];
        
        if bytes.len() == 32 {
            secret_key_array.copy_from_slice(bytes);
            return Ok(ED25519SecretKey(secret_key_array))
        }
        else {
            return Err(SlugErrors::InvalidLengthFromBytes)
        }
    }
    /// to usable type
    pub fn to_usable_type(&self) -> SigningKey {
        SigningKey::from_bytes(&self.0)
    }
    /// into public key
    pub fn public_key(&self) -> Result<ED25519PublicKey,SignatureError> {
        let vk = self.to_usable_type().verifying_key();
        Ok(ED25519PublicKey(vk.to_bytes()))
    }
    /// # Signing
    /// 
    /// Signs a message or data.
    pub fn sign<T: AsRef<[u8]>>(&self, msg: T) -> Result<ED25519Signature,SignatureError> {
        let signature = self.to_usable_type().try_sign(msg.as_ref())?;


        return Ok(ED25519Signature(signature.to_bytes()))
    }
    /// # To PEM
    /// 
    /// Encode to PEM format
    pub fn to_pem(&self) -> String {
        let pem = Pem::new("ED25519 Secret Key", &self.0);
        let x = pem::encode(&pem);
        return x
    }
    /// # From PEM
    /// 
    /// Decode from PEM format
    pub fn from_pem<T: AsRef<str>>(pem_str: T) -> Result<Self,SlugErrors> {
        let pem = pem::parse(pem_str.as_ref()).unwrap();
        if pem.tag() != "ED25519 Secret Key" {
            return Err(SlugErrors::Other(String::from("ED25519 From Pem Failure")))
        }
        ED25519SecretKey::from_bytes(&pem.contents())
    }
    pub fn from_slice(bytes: &[u8]) -> Result<Self,SlugErrors> {
        let mut x: [u8;32] = [0u8;32];

        if bytes.len() == 32 {
            x.copy_from_slice(bytes);
        }
        else {
            return Err(SlugErrors::InvalidLengthFromBytes)
        }
        Ok(Self::from_bytes(&x).unwrap())
    }
    pub fn to_hexadecimal(&self) -> Result<String,SlugEncodingError> {
        let x = SlugEncodingUsage::new(SlugEncodings::Hex).encode(&self.0)?;
        Ok(x)
    }
    pub fn to_base32(&self) -> Result<String,SlugEncodingError> {
        let x = SlugEncodingUsage::new(SlugEncodings::Base32).encode(&self.0)?;
        Ok(x)
    }
    pub fn to_base58(&self) -> Result<String,SlugEncodingError> {
        let x = SlugEncodingUsage::new(SlugEncodings::Base58).encode(&self.0)?;
        Ok(x)
    }
    pub fn to_base64(&self) -> Result<String,SlugEncodingError> {
        let x = SlugEncodingUsage::new(SlugEncodings::Base64).encode(&self.0)?;
        Ok(x)
    }
    pub fn to_base64_url_safe(&self) -> Result<String,SlugEncodingError> {
        let x = SlugEncodingUsage::new(SlugEncodings::Base64urlsafe).encode(&self.0)?;
        Ok(x)
    }
    pub fn to_base32_unpadded(&self) -> Result<String,SlugEncodingError> {
        let x = SlugEncodingUsage::new(SlugEncodings::Base32unpadded).encode(&self.0)?;
        Ok(x)
    }
    pub fn from_hex<T: AsRef<str>>(s: T) -> Result<Self,SlugEncodingError> {
        let bytes = SlugEncodingUsage::new(SlugEncodings::Hex).decode(s.as_ref())?;
        Ok(Self::from_bytes(&bytes).unwrap())
    }
    pub fn from_base32<T: AsRef<str>>(s: T) -> Result<Self,SlugEncodingError> {
        let bytes = SlugEncodingUsage::new(SlugEncodings::Base32).decode(s.as_ref())?;
        Ok(Self::from_bytes(&bytes).unwrap())
    }
    pub fn from_base58<T: AsRef<str>>(s: T) -> Result<Self,SlugEncodingError> {
        let bytes = SlugEncodingUsage::new(SlugEncodings::Base58).decode(s.as_ref())?;
        Ok(Self::from_bytes(&bytes).unwrap())
    }
    pub fn from_base64<T: AsRef<str>>(s: T) -> Result<Self,SlugEncodingError> {
        let bytes = SlugEncodingUsage::new(SlugEncodings::Base64).decode(s.as_ref())?;
        Ok(Self::from_bytes(&bytes).unwrap())
    }
    pub fn from_base64_url_safe<T: AsRef<str>>(s: T) -> Result<Self,SlugEncodingError> {
        let bytes = SlugEncodingUsage::new(SlugEncodings::Base64urlsafe).decode(s.as_ref())?;
        Ok(Self::from_bytes(&bytes).unwrap())
    }
    pub fn from_base32_unpadded<T: AsRef<str>>(s: T) -> Result<Self,SlugEncodingError> {
        let bytes = SlugEncodingUsage::new(SlugEncodings::Base32unpadded).decode(s.as_ref())?;
        Ok(Self::from_bytes(&bytes).unwrap())
    }
    pub fn public_key_bytes(&self) -> Result<[u8;32],SignatureError> {
        let vk = self.to_usable_type().verifying_key();
        Ok(vk.to_bytes())
    }
}

impl ED25519PublicKey {
    /// as bytes
    pub fn as_bytes(&self) -> &[u8] {
        &self.0
    }
    /// to byte array (32 bytes)
    pub fn to_bytes(&self) -> [u8;32] {
        self.0
    }
    /// from bytes (byte array of 32 bytes)
    pub fn from_bytes(bytes: [u8;32]) -> Self {
        Self(bytes)
    }
    pub fn from_slice(bytes: &[u8]) -> Result<Self,SlugErrors> {
        let mut x: [u8;32] = [0u8;32];

        if bytes.len() == 32 {
            x.copy_from_slice(bytes);
        }
        else {
            return Err(SlugErrors::InvalidLengthFromBytes)
        }
        Ok(Self::from_bytes(x))
    }
    /// to usable type
    fn to_usable_type(&self) -> Result<VerifyingKey,SignatureError> {
        VerifyingKey::from_bytes(&self.0)
    }
    /// # Verify (ED25519)
    /// 
    /// Verify a signature and message
    pub fn verify<T: AsRef<[u8]>>(&self, signature: ED25519Signature, msg: T) -> Result<bool,SignatureError> {
        let x = self.to_usable_type().unwrap().verify_strict(msg.as_ref(), &signature.to_usable_type())?;
        return Ok(true)
    }
    /// \[Encoding] Encode From UPPER-HEXADECIMAL
    pub fn to_hex_string(&self) -> String {
        String::from_utf8(hex::encode_upper(self.0)).unwrap()
    }
    /// \[Encoding] Decode From UPPER-HEXADECIMAL
    pub fn from_hex_string<T: AsRef<str>>(hex_str: T) -> Result<Vec<u8>,Error> {
        let bytes = hex::decode_upper(hex_str.as_ref().as_bytes())?;
        Ok(bytes)
    }
    /// \[Encoding] To Base32 string
    pub fn to_base32_string(&self) -> String {
        base32::encode(base32::Alphabet::Crockford, &self.0)
    }
    /// \[Encoding] From Base32 String
    pub fn from_base32_string<T: AsRef<str>>(bs32_str: T) -> Vec<u8> {
        let bytes = base32::decode(base32::Alphabet::Crockford, bs32_str.as_ref()).unwrap();
        return bytes
    }
    /// # To PEM
    pub fn to_pem(&self) -> String {
        let pem = Pem::new("ED25519 Public Key", &self.0);
        let x = pem::encode(&pem);
        return x
    }
    /// # From PEM
    pub fn from_pem<T: AsRef<str>>(pem_str: T) -> Result<Self,SlugErrors> {
        let pem = pem::parse(pem_str.as_ref()).unwrap();
        if pem.tag() != "ED25519 Public Key" {
            return Err(SlugErrors::Other(String::from("ED25519 From Pem Failure")))
        }
        return Ok(ED25519PublicKey::from_slice(&pem.contents()).unwrap())
    }
    pub fn to_hexadecimal(&self) -> Result<String,SlugEncodingError> {
        let x = SlugEncodingUsage::new(SlugEncodings::Hex).encode(&self.0)?;
        Ok(x)
    }
    pub fn to_base32(&self) -> Result<String,SlugEncodingError> {
        let x = SlugEncodingUsage::new(SlugEncodings::Base32).encode(&self.0)?;
        Ok(x)
    }
    pub fn to_base58(&self) -> Result<String,SlugEncodingError> {
        let x = SlugEncodingUsage::new(SlugEncodings::Base58).encode(&self.0)?;
        Ok(x)
    }
    pub fn to_base64(&self) -> Result<String,SlugEncodingError> {
        let x = SlugEncodingUsage::new(SlugEncodings::Base64).encode(&self.0)?;
        Ok(x)
    }
    pub fn to_base64_url_safe(&self) -> Result<String,SlugEncodingError> {
        let x = SlugEncodingUsage::new(SlugEncodings::Base64urlsafe).encode(&self.0)?;
        Ok(x)
    }
    pub fn to_base32_unpadded(&self) -> Result<String,SlugEncodingError> {
        let x = SlugEncodingUsage::new(SlugEncodings::Base32unpadded).encode(&self.0)?;
        Ok(x)
    }
    pub fn from_hex<T: AsRef<str>>(s: T) -> Result<Self,SlugEncodingError> {
        let bytes = SlugEncodingUsage::new(SlugEncodings::Hex).decode(s.as_ref())?;
        Ok(Self::from_slice(&bytes).unwrap())
    }
    pub fn from_base32<T: AsRef<str>>(s: T) -> Result<Self,SlugEncodingError> {
        let bytes = SlugEncodingUsage::new(SlugEncodings::Base32).decode(s.as_ref())?;
        Ok(Self::from_slice(&bytes).unwrap())
    }
    pub fn from_base58<T: AsRef<str>>(s: T) -> Result<Self,SlugEncodingError> {
        let bytes = SlugEncodingUsage::new(SlugEncodings::Base58).decode(s.as_ref())?;
        Ok(Self::from_slice(&bytes).unwrap())
    }
    pub fn from_base64<T: AsRef<str>>(s: T) -> Result<Self,SlugEncodingError> {
        let bytes = SlugEncodingUsage::new(SlugEncodings::Base64).decode(s.as_ref())?;
        Ok(Self::from_slice(&bytes).unwrap())
    }
    pub fn from_base64_url_safe<T: AsRef<str>>(s: T) -> Result<Self,SlugEncodingError> {
        let bytes = SlugEncodingUsage::new(SlugEncodings::Base64urlsafe).decode(s.as_ref())?;
        Ok(Self::from_slice(&bytes).unwrap())
    }
    pub fn from_base32_unpadded<T: AsRef<str>>(s: T) -> Result<Self,SlugEncodingError> {
        let bytes = SlugEncodingUsage::new(SlugEncodings::Base32unpadded).decode(s.as_ref())?;
        Ok(Self::from_slice(&bytes).unwrap())
    }
}

impl ED25519Signature {
    /// as bytes
    pub fn as_bytes(&self) -> &[u8] {
        &self.0
    }
    /// to 64-byte array
    pub fn to_bytes(&self) -> [u8;64] {
        self.0
    }
    /// from byte slice (must be 64 bytes)
    pub fn from_bytes(bytes: &[u8]) -> Result<Self,SlugErrors> {
        let mut signature_array: [u8;64] = [0u8;64];
        
        if bytes.len() == 64 {
            signature_array.copy_from_slice(bytes);
            return Ok(Self(signature_array))
        }
        else {
            return Err(SlugErrors::InvalidLengthFromBytes)
        }
    }
    /// to usable type
    pub fn to_usable_type(&self) -> Signature {
        Signature::from_bytes(&self.0)
    }
    /// to base58 string
    pub fn to_base58_string(&self) -> String {
        self.0.to_base58()
    }
    /// from base58 string (must convert into bytes)
    pub fn from_base58_string<T: AsRef<str>>(base58_str: T) -> Result<Vec<u8>,FromBase58Error> {
        let bytes = base58_str.as_ref().from_base58()?;
        Ok(bytes)
    }
    /// [Encoding] Encode From UPPER-HEXADECIMAL
    pub fn to_hex_string(&self) -> String {
        String::from_utf8(hex::encode_upper(self.0)).unwrap()
    }
    /// [Encoding] Decode From UPPER-HEXADECIMAL (must convert from bytes)
    pub fn from_hex_string<T: AsRef<str>>(hex_str: T) -> Result<Vec<u8>,Error> {
        let bytes = hex::decode_upper(hex_str.as_ref().as_bytes())?;
        Ok(bytes)
    }
    pub fn from_pem<T: AsRef<str>>(pem_str: T) -> Result<Self,SlugErrors> {
        let pem = pem::parse(pem_str.as_ref()).unwrap();
        if pem.tag() != "ED25519 Signature" {
            return Err(SlugErrors::Other(String::from("ED25519 From Pem Failure")))
        }
        ED25519Signature::from_bytes(&pem.contents())
    }
    pub fn to_pem(&self) -> String {
        let pem = Pem::new("ED25519 Signature", &self.0);
        let x = pem::encode(&pem);
        return x
    }
    pub fn to_hexadecimal(&self) -> Result<String,SlugEncodingError> {
        let x = SlugEncodingUsage::new(SlugEncodings::Hex).encode(&self.0)?;
        Ok(x)
    }
    pub fn to_base32(&self) -> Result<String,SlugEncodingError> {
        let x = SlugEncodingUsage::new(SlugEncodings::Base32).encode(&self.0)?;
        Ok(x)
    }
    pub fn to_base58(&self) -> Result<String,SlugEncodingError> {
        let x = SlugEncodingUsage::new(SlugEncodings::Base58).encode(&self.0)?;
        Ok(x)
    }
    pub fn to_base64(&self) -> Result<String,SlugEncodingError> {
        let x = SlugEncodingUsage::new(SlugEncodings::Base64).encode(&self.0)?;
        Ok(x)
    }
    pub fn to_base64_url_safe(&self) -> Result<String,SlugEncodingError> {
        let x = SlugEncodingUsage::new(SlugEncodings::Base64urlsafe).encode(&self.0)?;
        Ok(x)
    }
    pub fn to_base32_unpadded(&self) -> Result<String,SlugEncodingError> {
        let x = SlugEncodingUsage::new(SlugEncodings::Base32unpadded).encode(&self.0)?;
        Ok(x)
    }
       pub fn from_hex<T: AsRef<str>>(s: T) -> Result<Self,SlugEncodingError> {
        let bytes = SlugEncodingUsage::new(SlugEncodings::Hex).decode(s.as_ref())?;
        Ok(Self::from_bytes(&bytes).unwrap())
    }
    pub fn from_base32<T: AsRef<str>>(s: T) -> Result<Self,SlugEncodingError> {
        let bytes = SlugEncodingUsage::new(SlugEncodings::Base32).decode(s.as_ref())?;
        Ok(Self::from_bytes(&bytes).unwrap())
    }
    pub fn from_base58<T: AsRef<str>>(s: T) -> Result<Self,SlugEncodingError> {
        let bytes = SlugEncodingUsage::new(SlugEncodings::Base58).decode(s.as_ref())?;
        Ok(Self::from_bytes(&bytes).unwrap())
    }
    pub fn from_base64<T: AsRef<str>>(s: T) -> Result<Self,SlugEncodingError> {
        let bytes = SlugEncodingUsage::new(SlugEncodings::Base64).decode(s.as_ref())?;
        Ok(Self::from_bytes(&bytes).unwrap())
    }
    pub fn from_base64_url_safe<T: AsRef<str>>(s: T) -> Result<Self,SlugEncodingError> {
        let bytes = SlugEncodingUsage::new(SlugEncodings::Base64urlsafe).decode(s.as_ref())?;
        Ok(Self::from_bytes(&bytes).unwrap())
    }
    pub fn from_base32_unpadded<T: AsRef<str>>(s: T) -> Result<Self,SlugEncodingError> {
        let bytes = SlugEncodingUsage::new(SlugEncodings::Base32unpadded).decode(s.as_ref())?;
        Ok(Self::from_bytes(&bytes).unwrap())
    }
}


impl IntoEncoding for ED25519PublicKey {
    fn into_hex(&self) -> Result<String,SlugErrors> {
        let encoder = SlugEncodingUsage::new(SlugEncodings::Hex);
        let output: String = encoder.encode(&self.0)?;
        Ok(output)
    }

    fn into_base32(&self) -> Result<String,SlugErrors> {
        let output: String = SlugEncodingUsage::new(SlugEncodings::Base32).encode(&self.0)?;
        Ok(output)
    }

    fn into_base32_unpadded(&self) -> Result<String,SlugErrors> {
        let output = SlugEncodingUsage::new(SlugEncodings::Base32unpadded).encode(&self.0)?;
        Ok(output)
    }

    fn into_base58(&self) -> Result<String,SlugErrors> {
        let output = SlugEncodingUsage::new(SlugEncodings::Base58).encode(&self.0)?;
        Ok(output)
    }

    fn into_base64(&self) -> Result<String,SlugErrors> {
        let output = SlugEncodingUsage::new(SlugEncodings::Base64).encode(&self.0)?;
        Ok(output)
    }

    fn into_base64_url_safe(&self) -> Result<String,SlugErrors> {
        let output = SlugEncodingUsage::new(SlugEncodings::Base64urlsafe).encode(&self.0)?;
        Ok(output)
    }
}
impl IntoEncoding for ED25519SecretKey {
    fn into_hex(&self) -> Result<std::string::String, SlugErrors> {
        let encoder = SlugEncodingUsage::new(SlugEncodings::Hex);
        let output: String = encoder.encode(&self.0)?;
        Ok(output)
    }

    fn into_base32(&self) -> Result<std::string::String, SlugErrors> {
        let output: String = SlugEncodingUsage::new(SlugEncodings::Base32).encode(&self.0)?;
        Ok(output)
    }

    fn into_base32_unpadded(&self) -> Result<std::string::String, SlugErrors> {
        let output = SlugEncodingUsage::new(SlugEncodings::Base32unpadded).encode(&self.0)?;
        Ok(output)
    }

    fn into_base58(&self) -> Result<std::string::String, SlugErrors> {
        let output = SlugEncodingUsage::new(SlugEncodings::Base58).encode(&self.0)?;
        Ok(output)
    }

    fn into_base64(&self) -> Result<std::string::String, SlugErrors> {
        let output = SlugEncodingUsage::new(SlugEncodings::Base64).encode(&self.0)?;
        Ok(output)
    }

    fn into_base64_url_safe(&self) -> Result<std::string::String, SlugErrors> {
        let output = SlugEncodingUsage::new(SlugEncodings::Base64urlsafe).encode(&self.0)?;
        Ok(output)
    }
}
impl IntoEncoding for ED25519Signature {
    fn into_hex(&self) -> Result<std::string::String, SlugErrors> {
        let encoder = SlugEncodingUsage::new(SlugEncodings::Hex);
        let output: String = encoder.encode(&self.0)?;
        Ok(output)
    }

    fn into_base32(&self) -> Result<std::string::String, SlugErrors> {
        let output: String = SlugEncodingUsage::new(SlugEncodings::Base32).encode(&self.0)?;
        Ok(output)
    }

    fn into_base32_unpadded(&self) -> Result<std::string::String, SlugErrors> {
        let output = SlugEncodingUsage::new(SlugEncodings::Base32unpadded).encode(&self.0)?;
        Ok(output)
    }

    fn into_base58(&self) -> Result<std::string::String, SlugErrors> {
        let output = SlugEncodingUsage::new(SlugEncodings::Base58).encode(&self.0)?;
        Ok(output)
    }

    fn into_base64(&self) -> Result<std::string::String, SlugErrors> {
        let output = SlugEncodingUsage::new(SlugEncodings::Base64).encode(&self.0)?;
        Ok(output)
    }

    fn into_base64_url_safe(&self) -> Result<std::string::String, SlugErrors> {
        let output = SlugEncodingUsage::new(SlugEncodings::Base64urlsafe).encode(&self.0)?;
        Ok(output)
    }
}

impl FromEncoding for ED25519PublicKey {
    fn from_hex<T: AsRef<str>>(hex_str: T) -> Result<Self, SlugErrors> {
        let bytes = SlugEncodingUsage::new(SlugEncodings::Hex).decode(hex_str.as_ref())?;
        let x = Self::from_slice(&bytes)?;
        Ok(x)
    }
    fn from_base32<T: AsRef<str>>(bs32_str: T) -> Result<Self, SlugErrors> {
        let bytes = SlugEncodingUsage::new(SlugEncodings::Base32).decode(bs32_str.as_ref())?;
        let x = Self::from_slice(&bytes)?;
        Ok(x)
    }
    fn from_base58<T: AsRef<str>>(base58_str: T) -> Result<Self, SlugErrors> {
        let bytes = SlugEncodingUsage::new(SlugEncodings::Base58).decode(base58_str.as_ref())?;
        let x = Self::from_slice(&bytes)?;
        Ok(x)
    }
    fn from_base64<T: AsRef<str>>(b64_str: T) -> Result<Self, SlugErrors> {
        let decoder = SlugEncodingUsage::new(SlugEncodings::Base64);
        let bytes = decoder.decode(b64_str.as_ref())?;
        let x = Self::from_slice(&bytes)?;
        Ok(x)
    }
    fn from_base64_url_safe<T: AsRef<str>>(b64u_str: T) -> Result<Self, SlugErrors> {
        let decoder = SlugEncodingUsage::new(SlugEncodings::Base64urlsafe);
        let bytes = decoder.decode(b64u_str.as_ref())?;
        let x = Self::from_slice(&bytes)?;
        Ok(x)
    }
    fn from_base32_unpadded<T: AsRef<str>>(bs32u_str: T) -> Result<Self, SlugErrors> {
        let decoder = SlugEncodingUsage::new(SlugEncodings::Base32unpadded);
        let bytes = decoder.decode(bs32u_str.as_ref())?;
        let x = Self::from_slice(&bytes)?;
        Ok(x)
    }
}
impl FromEncoding for ED25519SecretKey {
    fn from_hex<T: AsRef<str>>(hex_str: T) -> Result<Self, SlugErrors> {
        let bytes = SlugEncodingUsage::new(SlugEncodings::Hex).decode(hex_str.as_ref())?;
        let x = Self::from_slice(&bytes)?;
        Ok(x)
    }
    fn from_base32<T: AsRef<str>>(bs32_str: T) -> Result<Self, SlugErrors> {
        let bytes = SlugEncodingUsage::new(SlugEncodings::Base32).decode(bs32_str.as_ref())?;
        let x = Self::from_slice(&bytes)?;
        Ok(x)
    }
    fn from_base58<T: AsRef<str>>(base58_str: T) -> Result<Self, SlugErrors> {
        let bytes = SlugEncodingUsage::new(SlugEncodings::Base58).decode(base58_str.as_ref())?;
        let x = Self::from_slice(&bytes)?;
        Ok(x)
    }
    fn from_base64<T: AsRef<str>>(b64_str: T) -> Result<Self, SlugErrors> {
        let decoder = SlugEncodingUsage::new(SlugEncodings::Base64);
        let bytes = decoder.decode(b64_str.as_ref())?;
        let x = Self::from_slice(&bytes)?;
        Ok(x)
    }
    fn from_base64_url_safe<T: AsRef<str>>(b64u_str: T) -> Result<Self, SlugErrors> {
        let decoder = SlugEncodingUsage::new(SlugEncodings::Base64urlsafe);
        let bytes = decoder.decode(b64u_str.as_ref())?;
        let x = Self::from_slice(&bytes)?;
        Ok(x)
    }
    fn from_base32_unpadded<T: AsRef<str>>(bs32u_str: T) -> Result<Self, SlugErrors> {
        let decoder = SlugEncodingUsage::new(SlugEncodings::Base32unpadded);
        let bytes = decoder.decode(bs32u_str.as_ref())?;
        let x = Self::from_slice(&bytes)?;
        Ok(x)
    }
}
impl FromEncoding for ED25519Signature {
    fn from_hex<T: AsRef<str>>(hex_str: T) -> Result<Self, SlugErrors> {
        let bytes = SlugEncodingUsage::new(SlugEncodings::Hex).decode(hex_str.as_ref())?;
        let x = Self::from_bytes(&bytes)?;
        Ok(x)
    }
    fn from_base32<T: AsRef<str>>(bs32_str: T) -> Result<Self, SlugErrors> {
        let bytes = SlugEncodingUsage::new(SlugEncodings::Base32).decode(bs32_str.as_ref())?;
        let x = Self::from_bytes(&bytes)?;
        Ok(x)
    }
    fn from_base58<T: AsRef<str>>(base58_str: T) -> Result<Self, SlugErrors> {
        let bytes = SlugEncodingUsage::new(SlugEncodings::Base58).decode(base58_str.as_ref())?;
        let x = Self::from_bytes(&bytes)?;
        Ok(x)
    }
    fn from_base64<T: AsRef<str>>(b64_str: T) -> Result<Self, SlugErrors> {
        let decoder = SlugEncodingUsage::new(SlugEncodings::Base64);
        let bytes = decoder.decode(b64_str.as_ref())?;
        let x = Self::from_bytes(&bytes)?;
        Ok(x)
    }
    fn from_base64_url_safe<T: AsRef<str>>(b64u_str: T) -> Result<Self, SlugErrors> {
        let decoder = SlugEncodingUsage::new(SlugEncodings::Base64urlsafe);
        let bytes = decoder.decode(b64u_str.as_ref())?;
        let x = Self::from_bytes(&bytes)?;
        Ok(x)
    }
    fn from_base32_unpadded<T: AsRef<str>>(bs32u_str: T) -> Result<Self, SlugErrors> {
        let decoder = SlugEncodingUsage::new(SlugEncodings::Base32unpadded);
        let bytes = decoder.decode(bs32u_str.as_ref())?;
        let x = Self::from_bytes(&bytes)?;
        Ok(x)
    }
}

impl IntoBincode for ED25519PublicKey {
    fn into_bincode(&self) -> Result<Vec<u8>, SlugErrors> {
        let encoded = bincode::serialize(&self)?;
        Ok(encoded)
    }
}

impl IntoBincode for ED25519SecretKey {
    fn into_bincode(&self) -> Result<Vec<u8>, SlugErrors> {
        let encoded = bincode::serialize(&self)?;
        Ok(encoded)
    }
}

impl IntoBincode for ED25519Signature {
    fn into_bincode(&self) -> Result<Vec<u8>, SlugErrors> {
        let encoded = bincode::serialize(&self)?;
        Ok(encoded)
    }
}

impl FromBincode for ED25519PublicKey {
    fn from_bincode<T: AsRef<[u8]>>(bytes: T) -> Result<Self, SlugErrors> {
        let decoded: Self = bincode::deserialize(bytes.as_ref())?;
        Ok(decoded)
    }
}

impl FromBincode for ED25519SecretKey {
    fn from_bincode<T: AsRef<[u8]>>(bytes: T) -> Result<Self, SlugErrors> {
        let decoded: Self = bincode::deserialize(bytes.as_ref())?;
        Ok(decoded)
    }
}

impl FromBincode for ED25519Signature {
    fn from_bincode<T: AsRef<[u8]>>(bytes: T) -> Result<Self, SlugErrors> {
        let decoded: Self = bincode::deserialize(bytes.as_ref())?;
        Ok(decoded)
    }
}

impl IntoStandardPem for ED25519PublicKey {
    fn into_standard_pem(&self) -> Result<String,SlugErrors> {
        let x = self.into_bincode()?;
        let pem = Pem::new(&Self::label_for_standard_pem(), x);
        return Ok(pem.to_string())
    }
    fn label_for_standard_pem() -> String {
        String::from("OpenInternetCryptographyProject/ED25519-Public-Key")
    }
    fn label_for_standard_pem_secret() -> String {
        String::from("OpenInternetCryptographyProject/ED25519-Secret-Key")
    }
}

impl IntoStandardPem for ED25519SecretKey {
    fn into_standard_pem(&self) -> Result<String,SlugErrors> {
        let x = self.into_bincode()?;
        let pem = Pem::new(&Self::label_for_standard_pem_secret(), x);
        return Ok(pem.to_string())
    }
    fn label_for_standard_pem() -> String {
        String::from("OpenInternetCryptographyProject/ED25519-Secret-Key")
    }
    fn label_for_standard_pem_secret() -> String {
        String::from("OpenInternetCryptographyProject/ED25519-Secret-Key")
    }
}

impl IntoStandardPem for ED25519Signature {
    fn into_standard_pem(&self) -> Result<String,SlugErrors> {
        let x = self.into_bincode()?;
        let pem = Pem::new(&Self::label_for_standard_pem(), x);
        return Ok(pem.to_string())
    }
    fn label_for_standard_pem() -> String {
        String::from("OpenInternetCryptographyProject/ED25519-Signature")
    }
    fn label_for_standard_pem_secret() -> String {
        String::from("OpenInternetCryptographyProject/ED25519-Secret-Key")
    }
}

impl FromStandardPem for ED25519PublicKey {
    fn from_standard_pem<T: AsRef<str>>(pem_str: T) -> Result<Self, SlugErrors> {
        let pem: Pem = Pem::from_str(pem_str.as_ref()).unwrap();
        if pem.tag() != Self::label_for_standard_pem() {
            return Err(SlugErrors::Other(String::from("ED25519 Public Key From Standard Pem Failure")))
        }
        let decoded: ED25519PublicKey = Self::from_bincode(&pem.contents())?;
        Ok(decoded)
    }
}

impl FromStandardPem for ED25519SecretKey {
    fn from_standard_pem<T: AsRef<str>>(pem_str: T) -> Result<Self, SlugErrors> {
        let pem: Pem = Pem::from_str(pem_str.as_ref()).unwrap();
        if pem.tag() != Self::label_for_standard_pem_secret() {
            return Err(SlugErrors::Other(String::from("ED25519 Secret Key From Standard Pem Failure")))
        }
        let decoded: ED25519SecretKey = Self::from_bincode(&pem.contents())?;
        Ok(decoded)
    }
}

impl FromStandardPem for ED25519Signature {
    fn from_standard_pem<T: AsRef<str>>(pem_str: T) -> Result<Self, SlugErrors> {
        let pem: Pem = Pem::from_str(pem_str.as_ref()).unwrap();
        if pem.tag() != Self::label_for_standard_pem() {
            return Err(SlugErrors::Other(String::from("ED25519 Signature From Standard Pem Failure")))
        }
        let decoded: ED25519Signature = Self::from_bincode(&pem.contents())?;
        Ok(decoded)
    }
}


#[test]
fn run() {
    let sk = ED25519SecretKey::generate();
    println!("Secret Key: {:?}", sk);
}

#[test]
fn ed25519() {
    let sk = ED25519SecretKey::generate();
    let cert = sk.public_key().unwrap();
}