github.com/iDigitalFlame/xmt@v0.5.4/data/crypto_no_implant.go (about) 1 //go:build !implant 2 // +build !implant 3 4 // Copyright (C) 2020 - 2023 iDigitalFlame 5 // 6 // This program is free software: you can redistribute it and/or modify 7 // it under the terms of the GNU General Public License as published by 8 // the Free Software Foundation, either version 3 of the License, or 9 // any later version. 10 // 11 // This program is distributed in the hope that it will be useful, 12 // but WITHOUT ANY WARRANTY; without even the implied warranty of 13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 // GNU General Public License for more details. 15 // 16 // You should have received a copy of the GNU General Public License 17 // along with this program. If not, see <https://www.gnu.org/licenses/>. 18 // 19 20 package data 21 22 import ( 23 "github.com/iDigitalFlame/xmt/util" 24 "github.com/iDigitalFlame/xmt/util/xerr" 25 ) 26 27 // String returns a colon ':' seperated version of the PublicKey's hex value. 28 func (p PublicKey) String() string { 29 var b [(publicKeySize * 3) - 1]byte 30 for i, n := 0, 0; i < len(p); i, n = i+1, n+2 { 31 if n > 0 { 32 b[n] = ':' 33 n++ 34 } 35 if p[i] < 16 { 36 b[n] = '0' 37 b[n+1] = util.HexTable[p[i]&0x0F] 38 } else { 39 b[n] = util.HexTable[p[i]>>4] 40 b[n+1] = util.HexTable[p[i]&0x0F] 41 } 42 } 43 return string(b[:]) 44 } 45 46 // String returns a colon ':' seperated version of the PrivateKey's hex value. 47 func (p PrivateKey) String() string { 48 var b [(privateKeySize * 3) - 1]byte 49 for i, n := 0, 0; i < len(p); i, n = i+1, n+2 { 50 if n > 0 { 51 b[n] = ':' 52 n++ 53 } 54 if p[i] < 16 { 55 b[n] = '0' 56 b[n+1] = util.HexTable[p[i]&0x0F] 57 } else { 58 b[n] = util.HexTable[p[i]>>4] 59 b[n+1] = util.HexTable[p[i]&0x0F] 60 } 61 } 62 return string(b[:]) 63 } 64 func hexByteToInt(v byte) (uint8, error) { 65 // Quick way to convert hex bytes to real numbers. 66 switch { 67 case v >= 0x61 && v <= 0x66: // a - f 68 return (v - 0x61) + 0xA, nil 69 case v >= 0x41 && v <= 0x46: // A - F 70 return (v - 0x41) + 0xA, nil 71 case v >= 0x30 && v <= 0x39: // 0 - 9 72 return v - 0x30, nil 73 } 74 return 0, xerr.Sub("invalid non-hex byte", 0x76) 75 } 76 77 // Parse will attempt to fill the data of this PublicKey from the supplied PublicKey 78 // colon-seperated hex string. 79 // 80 // This function will only overrite the PublicKey data if the entire parsing 81 // process succeeds. 82 // 83 // Any errors occurred during parsing will be returned. 84 func (p *PublicKey) Parse(v string) error { 85 if len(v) != (publicKeySize*3)-1 { 86 return xerr.Sub("invalid hex string length", 0x74) 87 } 88 var ( 89 b [publicKeySize]byte 90 f, s byte 91 err error 92 ) 93 for i, n := 0, 0; i < (publicKeySize*3)-1; i, n = i+2, n+1 { 94 if i > 0 { 95 if v[i] != ':' { 96 return xerr.Sub("invalid non-colon character", 0x75) 97 } 98 i++ 99 } 100 if f, err = hexByteToInt(v[i]); err != nil { 101 return err 102 } 103 if s, err = hexByteToInt(v[i+1]); err != nil { 104 return err 105 } 106 b[n] = (f << 0x4) | s 107 } 108 copy((*p)[:], b[:]) 109 return nil 110 } 111 112 // Parse will attempt to fill the data of this PrivateKey from the supplied PublicKey 113 // colon-seperated hex string. 114 // 115 // This function will only overrite the PrivateKey data if the entire parsing 116 // process succeeds. 117 // 118 // Any errors occurred during parsing will be returned. 119 func (p *PrivateKey) Parse(v string) error { 120 if len(v) != (privateKeySize*3)-1 { 121 return xerr.Sub("invalid hex string length", 0x74) 122 } 123 var ( 124 b [privateKeySize]byte 125 f, s byte 126 err error 127 ) 128 for i, n := 0, 0; i < (privateKeySize*3)-1; i, n = i+2, n+1 { 129 if i > 0 { 130 if v[i] != ':' { 131 return xerr.Sub("invalid non-colon character", 0x75) 132 } 133 i++ 134 } 135 if f, err = hexByteToInt(v[i]); err != nil { 136 return err 137 } 138 if s, err = hexByteToInt(v[i+1]); err != nil { 139 return err 140 } 141 b[n] = (f << 0x4) | s 142 } 143 copy((*p)[:], b[:]) 144 return nil 145 }