prefer endians2 (#98)

This commit is contained in:
Jacek Sieka 2020-03-10 21:40:52 +01:00 committed by GitHub
parent 8c96de5397
commit 52cb7ced04
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 2 additions and 17 deletions

View File

@ -8,7 +8,7 @@
## those terms. ## those terms.
## This module implements minimal ASN.1 encoding/decoding primitives. ## This module implements minimal ASN.1 encoding/decoding primitives.
import endians import stew/endians2
import nimcrypto/utils import nimcrypto/utils
type type
@ -218,22 +218,7 @@ proc asn1EncodeInteger*[T: SomeUnsignedInt](dest: var openarray[byte],
## If length of ``dest`` is less then number of required bytes to encode ## If length of ``dest`` is less then number of required bytes to encode
## ``value``, then result of encoding will not be stored in ``dest`` ## ``value``, then result of encoding will not be stored in ``dest``
## but number of bytes (octets) required will be returned. ## but number of bytes (octets) required will be returned.
when T is uint64: dest.asn1EncodeInteger(value.toBytesBE())
var buffer: array[8, byte]
bigEndian64(addr buffer[0], cast[pointer](unsafeAddr value))
result = asn1EncodeInteger(dest, buffer)
elif T is uint32:
var buffer: array[4, byte]
bigEndian32(addr buffer[0], cast[pointer](unsafeAddr value))
result = asn1EncodeInteger(dest, buffer)
elif T is uint16:
var buffer: array[2, byte]
bigEndian16(addr buffer[0], cast[pointer](unsafeAddr value))
result = asn1EncodeInteger(dest, buffer)
elif T is uint8:
var buffer: array[1, byte]
buffer[0] = value
result = asn1EncodeInteger(dest, buffer)
proc asn1EncodeBoolean*(dest: var openarray[byte], value: bool): int = proc asn1EncodeBoolean*(dest: var openarray[byte], value: bool): int =
## Encode Nim's boolean as ASN.1 DER `BOOLEAN` and return number of bytes ## Encode Nim's boolean as ASN.1 DER `BOOLEAN` and return number of bytes