1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

crypto.pem: add a static method Block.new, to replace new (#18846)

This commit is contained in:
phoebe 2023-07-18 07:02:30 +02:00 committed by GitHub
parent 39cfaafaae
commit 6dcf122172
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 64 additions and 42 deletions

View File

@ -2,14 +2,12 @@ module pem
import encoding.base64
// `decode` reads `data` and returns the first parsed PEM Block along with the rest of
// decode reads `data` and returns the first parsed PEM Block along with the rest of
// the string. `none` is returned when a header is expected, but not present
// or when a start of '-----BEGIN' or end of '-----END' can't be found in `data`
pub fn decode(data string) ?(Block, string) {
mut rest := data[data.index(pem_begin)?..]
mut block := Block{
block_type: rest[pem_begin.len..].all_before(pem_eol)
}
mut block := Block.new(rest[pem_begin.len..].all_before(pem_eol))
block.headers, rest = parse_headers(rest[pem_begin.len..].all_after(pem_eol).trim_left(' \n\t\v\f\r'))?
block_end_index := rest.index(pem_end)?

View File

@ -3,7 +3,7 @@ module pem
import encoding.base64
import arrays
// `encode_config` encodes the given block into a
// encode encodes the given block into a
// string using the EncodeConfig. It returns an error if `block_type` is undefined
// or if a value in `headers` contains an invalid character ':'
//

View File

@ -9,6 +9,13 @@ const (
colon = ':'
)
// new returns a new `Block` with the specified block_type
[deprecated: 'use Block.new instead']
[inline]
pub fn new(block_type string) Block {
return Block.new(block_type)
}
[params]
pub struct EncodeConfig {
pub mut:
@ -34,7 +41,7 @@ pub enum Header {
crl
}
// `str` returns the string representation of the header
// str returns the string representation of the header
pub fn (header Header) str() string {
return match header {
.proctype { 'Proc-Type' }
@ -62,6 +69,14 @@ pub mut:
data []u8
}
// Block.new returns a new `Block` with the specified block_type
[inline]
pub fn Block.new(block_type string) Block {
return Block{
block_type: block_type
}
}
// free the resources taken by the Block `block`
[unsafe]
pub fn (mut block Block) free() {
@ -75,15 +90,7 @@ pub fn (mut block Block) free() {
}
}
// returns a new `Block` with the specified block_type
[inline]
pub fn new(block_type string) Block {
return Block{
block_type: block_type
}
}
// returns the selected key using the Header enum
// header_by_key returns the selected key using the Header enum
//
// same as `block.headers[key.str()]`
[inline]

View File

@ -44,6 +44,23 @@ fn test_encode_config() {
}
}
fn test_decode_no_pem() {
for test in pem.test_data_no_pem {
if _, _ := decode(test) {
assert false, 'Block.decode_partial should return `none` on input without PEM data'
} else {
assert true
}
}
}
const test_data_no_pem = [
'',
'-----BEGIN',
'-----BEGIN -----',
'-----END',
]
// https://datatracker.ietf.org/doc/html/rfc7468#section-4
const test_data_rfc1421 = [
'-----BEGIN PRIVACY-ENHANCED MESSAGE-----